efi: Generalize relocate_kernel() for use by other architectures.
[firefly-linux-kernel-4.4.55.git] / drivers / firmware / efi / efi-stub-helper.c
1 /*
2  * Helper functions used by the EFI stub on multiple
3  * architectures. This should be #included by the EFI stub
4  * implementation files.
5  *
6  * Copyright 2011 Intel Corporation; author Matt Fleming
7  *
8  * This file is part of the Linux kernel, and is made available
9  * under the terms of the GNU General Public License version 2.
10  *
11  */
12 #define EFI_READ_CHUNK_SIZE     (1024 * 1024)
13
14 struct initrd {
15         efi_file_handle_t *handle;
16         u64 size;
17 };
18
19
20
21
22 static void efi_char16_printk(efi_system_table_t *sys_table_arg,
23                               efi_char16_t *str)
24 {
25         struct efi_simple_text_output_protocol *out;
26
27         out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
28         efi_call_phys2(out->output_string, out, str);
29 }
30
31 static void efi_printk(efi_system_table_t *sys_table_arg, char *str)
32 {
33         char *s8;
34
35         for (s8 = str; *s8; s8++) {
36                 efi_char16_t ch[2] = { 0 };
37
38                 ch[0] = *s8;
39                 if (*s8 == '\n') {
40                         efi_char16_t nl[2] = { '\r', 0 };
41                         efi_char16_printk(sys_table_arg, nl);
42                 }
43
44                 efi_char16_printk(sys_table_arg, ch);
45         }
46 }
47
48
49 static efi_status_t __get_map(efi_system_table_t *sys_table_arg,
50                               efi_memory_desc_t **map,
51                               unsigned long *map_size,
52                               unsigned long *desc_size)
53 {
54         efi_memory_desc_t *m = NULL;
55         efi_status_t status;
56         unsigned long key;
57         u32 desc_version;
58
59         *map_size = sizeof(*m) * 32;
60 again:
61         /*
62          * Add an additional efi_memory_desc_t because we're doing an
63          * allocation which may be in a new descriptor region.
64          */
65         *map_size += sizeof(*m);
66         status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
67                                 EFI_LOADER_DATA, *map_size, (void **)&m);
68         if (status != EFI_SUCCESS)
69                 goto fail;
70
71         status = efi_call_phys5(sys_table_arg->boottime->get_memory_map,
72                                 map_size, m, &key, desc_size, &desc_version);
73         if (status == EFI_BUFFER_TOO_SMALL) {
74                 efi_call_phys1(sys_table_arg->boottime->free_pool, m);
75                 goto again;
76         }
77
78         if (status != EFI_SUCCESS)
79                 efi_call_phys1(sys_table_arg->boottime->free_pool, m);
80
81 fail:
82         *map = m;
83         return status;
84 }
85
86 /*
87  * Allocate at the highest possible address that is not above 'max'.
88  */
89 static efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
90                                unsigned long size, unsigned long align,
91                                unsigned long *addr, unsigned long max)
92 {
93         unsigned long map_size, desc_size;
94         efi_memory_desc_t *map;
95         efi_status_t status;
96         unsigned long nr_pages;
97         u64 max_addr = 0;
98         int i;
99
100         status = __get_map(sys_table_arg, &map, &map_size, &desc_size);
101         if (status != EFI_SUCCESS)
102                 goto fail;
103
104         /*
105          * Enforce minimum alignment that EFI requires when requesting
106          * a specific address.  We are doing page-based allocations,
107          * so we must be aligned to a page.
108          */
109         if (align < EFI_PAGE_SIZE)
110                 align = EFI_PAGE_SIZE;
111
112         nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
113 again:
114         for (i = 0; i < map_size / desc_size; i++) {
115                 efi_memory_desc_t *desc;
116                 unsigned long m = (unsigned long)map;
117                 u64 start, end;
118
119                 desc = (efi_memory_desc_t *)(m + (i * desc_size));
120                 if (desc->type != EFI_CONVENTIONAL_MEMORY)
121                         continue;
122
123                 if (desc->num_pages < nr_pages)
124                         continue;
125
126                 start = desc->phys_addr;
127                 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
128
129                 if ((start + size) > end || (start + size) > max)
130                         continue;
131
132                 if (end - size > max)
133                         end = max;
134
135                 if (round_down(end - size, align) < start)
136                         continue;
137
138                 start = round_down(end - size, align);
139
140                 /*
141                  * Don't allocate at 0x0. It will confuse code that
142                  * checks pointers against NULL.
143                  */
144                 if (start == 0x0)
145                         continue;
146
147                 if (start > max_addr)
148                         max_addr = start;
149         }
150
151         if (!max_addr)
152                 status = EFI_NOT_FOUND;
153         else {
154                 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
155                                         EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
156                                         nr_pages, &max_addr);
157                 if (status != EFI_SUCCESS) {
158                         max = max_addr;
159                         max_addr = 0;
160                         goto again;
161                 }
162
163                 *addr = max_addr;
164         }
165
166 free_pool:
167         efi_call_phys1(sys_table_arg->boottime->free_pool, map);
168
169 fail:
170         return status;
171 }
172
173 /*
174  * Allocate at the lowest possible address.
175  */
176 static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
177                               unsigned long size, unsigned long align,
178                               unsigned long *addr)
179 {
180         unsigned long map_size, desc_size;
181         efi_memory_desc_t *map;
182         efi_status_t status;
183         unsigned long nr_pages;
184         int i;
185
186         status = __get_map(sys_table_arg, &map, &map_size, &desc_size);
187         if (status != EFI_SUCCESS)
188                 goto fail;
189
190         /*
191          * Enforce minimum alignment that EFI requires when requesting
192          * a specific address.  We are doing page-based allocations,
193          * so we must be aligned to a page.
194          */
195         if (align < EFI_PAGE_SIZE)
196                 align = EFI_PAGE_SIZE;
197
198         nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
199         for (i = 0; i < map_size / desc_size; i++) {
200                 efi_memory_desc_t *desc;
201                 unsigned long m = (unsigned long)map;
202                 u64 start, end;
203
204                 desc = (efi_memory_desc_t *)(m + (i * desc_size));
205
206                 if (desc->type != EFI_CONVENTIONAL_MEMORY)
207                         continue;
208
209                 if (desc->num_pages < nr_pages)
210                         continue;
211
212                 start = desc->phys_addr;
213                 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
214
215                 /*
216                  * Don't allocate at 0x0. It will confuse code that
217                  * checks pointers against NULL. Skip the first 8
218                  * bytes so we start at a nice even number.
219                  */
220                 if (start == 0x0)
221                         start += 8;
222
223                 start = round_up(start, align);
224                 if ((start + size) > end)
225                         continue;
226
227                 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
228                                         EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
229                                         nr_pages, &start);
230                 if (status == EFI_SUCCESS) {
231                         *addr = start;
232                         break;
233                 }
234         }
235
236         if (i == map_size / desc_size)
237                 status = EFI_NOT_FOUND;
238
239 free_pool:
240         efi_call_phys1(sys_table_arg->boottime->free_pool, map);
241 fail:
242         return status;
243 }
244
245 static void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
246                      unsigned long addr)
247 {
248         unsigned long nr_pages;
249
250         nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
251         efi_call_phys2(sys_table_arg->boottime->free_pages, addr, nr_pages);
252 }
253
254
255 /*
256  * Check the cmdline for a LILO-style initrd= arguments.
257  *
258  * We only support loading an initrd from the same filesystem as the
259  * kernel image.
260  */
261 static efi_status_t handle_ramdisks(efi_system_table_t *sys_table_arg,
262                                     efi_loaded_image_t *image,
263                                     struct setup_header *hdr)
264 {
265         struct initrd *initrds;
266         unsigned long initrd_addr;
267         efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
268         u64 initrd_total;
269         efi_file_io_interface_t *io;
270         efi_file_handle_t *fh;
271         efi_status_t status;
272         int nr_initrds;
273         char *str;
274         int i, j, k;
275
276         initrd_addr = 0;
277         initrd_total = 0;
278
279         str = (char *)(unsigned long)hdr->cmd_line_ptr;
280
281         j = 0;                  /* See close_handles */
282
283         if (!str || !*str)
284                 return EFI_SUCCESS;
285
286         for (nr_initrds = 0; *str; nr_initrds++) {
287                 str = strstr(str, "initrd=");
288                 if (!str)
289                         break;
290
291                 str += 7;
292
293                 /* Skip any leading slashes */
294                 while (*str == '/' || *str == '\\')
295                         str++;
296
297                 while (*str && *str != ' ' && *str != '\n')
298                         str++;
299         }
300
301         if (!nr_initrds)
302                 return EFI_SUCCESS;
303
304         status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
305                                 EFI_LOADER_DATA,
306                                 nr_initrds * sizeof(*initrds),
307                                 &initrds);
308         if (status != EFI_SUCCESS) {
309                 efi_printk(sys_table_arg, "Failed to alloc mem for initrds\n");
310                 goto fail;
311         }
312
313         str = (char *)(unsigned long)hdr->cmd_line_ptr;
314         for (i = 0; i < nr_initrds; i++) {
315                 struct initrd *initrd;
316                 efi_file_handle_t *h;
317                 efi_file_info_t *info;
318                 efi_char16_t filename_16[256];
319                 unsigned long info_sz;
320                 efi_guid_t info_guid = EFI_FILE_INFO_ID;
321                 efi_char16_t *p;
322                 u64 file_sz;
323
324                 str = strstr(str, "initrd=");
325                 if (!str)
326                         break;
327
328                 str += 7;
329
330                 initrd = &initrds[i];
331                 p = filename_16;
332
333                 /* Skip any leading slashes */
334                 while (*str == '/' || *str == '\\')
335                         str++;
336
337                 while (*str && *str != ' ' && *str != '\n') {
338                         if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
339                                 break;
340
341                         if (*str == '/') {
342                                 *p++ = '\\';
343                                 *str++;
344                         } else {
345                                 *p++ = *str++;
346                         }
347                 }
348
349                 *p = '\0';
350
351                 /* Only open the volume once. */
352                 if (!i) {
353                         efi_boot_services_t *boottime;
354
355                         boottime = sys_table_arg->boottime;
356
357                         status = efi_call_phys3(boottime->handle_protocol,
358                                         image->device_handle, &fs_proto, &io);
359                         if (status != EFI_SUCCESS) {
360                                 efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
361                                 goto free_initrds;
362                         }
363
364                         status = efi_call_phys2(io->open_volume, io, &fh);
365                         if (status != EFI_SUCCESS) {
366                                 efi_printk(sys_table_arg, "Failed to open volume\n");
367                                 goto free_initrds;
368                         }
369                 }
370
371                 status = efi_call_phys5(fh->open, fh, &h, filename_16,
372                                         EFI_FILE_MODE_READ, (u64)0);
373                 if (status != EFI_SUCCESS) {
374                         efi_printk(sys_table_arg, "Failed to open initrd file: ");
375                         efi_char16_printk(sys_table_arg, filename_16);
376                         efi_printk(sys_table_arg, "\n");
377                         goto close_handles;
378                 }
379
380                 initrd->handle = h;
381
382                 info_sz = 0;
383                 status = efi_call_phys4(h->get_info, h, &info_guid,
384                                         &info_sz, NULL);
385                 if (status != EFI_BUFFER_TOO_SMALL) {
386                         efi_printk(sys_table_arg, "Failed to get initrd info size\n");
387                         goto close_handles;
388                 }
389
390 grow:
391                 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
392                                         EFI_LOADER_DATA, info_sz, &info);
393                 if (status != EFI_SUCCESS) {
394                         efi_printk(sys_table_arg, "Failed to alloc mem for initrd info\n");
395                         goto close_handles;
396                 }
397
398                 status = efi_call_phys4(h->get_info, h, &info_guid,
399                                         &info_sz, info);
400                 if (status == EFI_BUFFER_TOO_SMALL) {
401                         efi_call_phys1(sys_table_arg->boottime->free_pool,
402                                        info);
403                         goto grow;
404                 }
405
406                 file_sz = info->file_size;
407                 efi_call_phys1(sys_table_arg->boottime->free_pool, info);
408
409                 if (status != EFI_SUCCESS) {
410                         efi_printk(sys_table_arg, "Failed to get initrd info\n");
411                         goto close_handles;
412                 }
413
414                 initrd->size = file_sz;
415                 initrd_total += file_sz;
416         }
417
418         if (initrd_total) {
419                 unsigned long addr;
420
421                 /*
422                  * Multiple initrd's need to be at consecutive
423                  * addresses in memory, so allocate enough memory for
424                  * all the initrd's.
425                  */
426                 status = efi_high_alloc(sys_table_arg, initrd_total, 0x1000,
427                                     &initrd_addr, hdr->initrd_addr_max);
428                 if (status != EFI_SUCCESS) {
429                         efi_printk(sys_table_arg, "Failed to alloc highmem for initrds\n");
430                         goto close_handles;
431                 }
432
433                 /* We've run out of free low memory. */
434                 if (initrd_addr > hdr->initrd_addr_max) {
435                         efi_printk(sys_table_arg, "We've run out of free low memory\n");
436                         status = EFI_INVALID_PARAMETER;
437                         goto free_initrd_total;
438                 }
439
440                 addr = initrd_addr;
441                 for (j = 0; j < nr_initrds; j++) {
442                         u64 size;
443
444                         size = initrds[j].size;
445                         while (size) {
446                                 u64 chunksize;
447                                 if (size > EFI_READ_CHUNK_SIZE)
448                                         chunksize = EFI_READ_CHUNK_SIZE;
449                                 else
450                                         chunksize = size;
451                                 status = efi_call_phys3(fh->read,
452                                                         initrds[j].handle,
453                                                         &chunksize, addr);
454                                 if (status != EFI_SUCCESS) {
455                                         efi_printk(sys_table_arg, "Failed to read initrd\n");
456                                         goto free_initrd_total;
457                                 }
458                                 addr += chunksize;
459                                 size -= chunksize;
460                         }
461
462                         efi_call_phys1(fh->close, initrds[j].handle);
463                 }
464
465         }
466
467         efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
468
469         hdr->ramdisk_image = initrd_addr;
470         hdr->ramdisk_size = initrd_total;
471
472         return status;
473
474 free_initrd_total:
475         efi_free(sys_table_arg, initrd_total, initrd_addr);
476
477 close_handles:
478         for (k = j; k < i; k++)
479                 efi_call_phys1(fh->close, initrds[k].handle);
480 free_initrds:
481         efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
482 fail:
483         hdr->ramdisk_image = 0;
484         hdr->ramdisk_size = 0;
485
486         return status;
487 }
488 /*
489  * Relocate a kernel image, either compressed or uncompressed.
490  * In the ARM64 case, all kernel images are currently
491  * uncompressed, and as such when we relocate it we need to
492  * allocate additional space for the BSS segment. Any low
493  * memory that this function should avoid needs to be
494  * unavailable in the EFI memory map, as if the preferred
495  * address is not available the lowest available address will
496  * be used.
497  */
498 static efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
499                                         unsigned long *image_addr,
500                                         unsigned long image_size,
501                                         unsigned long alloc_size,
502                                         unsigned long preferred_addr,
503                                         unsigned long alignment)
504 {
505         unsigned long cur_image_addr;
506         unsigned long new_addr = 0;
507         efi_status_t status;
508         unsigned long nr_pages;
509         efi_physical_addr_t efi_addr = preferred_addr;
510
511         if (!image_addr || !image_size || !alloc_size)
512                 return EFI_INVALID_PARAMETER;
513         if (alloc_size < image_size)
514                 return EFI_INVALID_PARAMETER;
515
516         cur_image_addr = *image_addr;
517
518         /*
519          * The EFI firmware loader could have placed the kernel image
520          * anywhere in memory, but the kernel has restrictions on the
521          * max physical address it can run at.  Some architectures
522          * also have a prefered address, so first try to relocate
523          * to the preferred address.  If that fails, allocate as low
524          * as possible while respecting the required alignment.
525          */
526         nr_pages = round_up(alloc_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
527         status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
528                                 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
529                                 nr_pages, &efi_addr);
530         new_addr = efi_addr;
531         /*
532          * If preferred address allocation failed allocate as low as
533          * possible.
534          */
535         if (status != EFI_SUCCESS) {
536                 status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
537                                        &new_addr);
538         }
539         if (status != EFI_SUCCESS) {
540                 efi_printk(sys_table_arg, "ERROR: Failed to allocate usable memory for kernel.\n");
541                 return status;
542         }
543
544         /*
545          * We know source/dest won't overlap since both memory ranges
546          * have been allocated by UEFI, so we can safely use memcpy.
547          */
548         memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
549         /* Zero any extra space we may have allocated for BSS. */
550         memset((void *)(new_addr + image_size), alloc_size - image_size, 0);
551
552         /* Return the new address of the relocated image. */
553         *image_addr = new_addr;
554
555         return status;
556 }