2 * Helper functions used by the EFI stub on multiple
3 * architectures. This should be #included by the EFI stub
4 * implementation files.
6 * Copyright 2011 Intel Corporation; author Matt Fleming
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.
12 #define EFI_READ_CHUNK_SIZE (1024 * 1024)
15 efi_file_handle_t *handle;
22 static void efi_char16_printk(efi_system_table_t *sys_table_arg,
25 struct efi_simple_text_output_protocol *out;
27 out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
28 efi_call_phys2(out->output_string, out, str);
31 static void efi_printk(efi_system_table_t *sys_table_arg, char *str)
35 for (s8 = str; *s8; s8++) {
36 efi_char16_t ch[2] = { 0 };
40 efi_char16_t nl[2] = { '\r', 0 };
41 efi_char16_printk(sys_table_arg, nl);
44 efi_char16_printk(sys_table_arg, ch);
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)
54 efi_memory_desc_t *m = NULL;
59 *map_size = sizeof(*m) * 32;
62 * Add an additional efi_memory_desc_t because we're doing an
63 * allocation which may be in a new descriptor region.
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)
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);
78 if (status != EFI_SUCCESS)
79 efi_call_phys1(sys_table_arg->boottime->free_pool, m);
87 * Allocate at the highest possible address that is not above 'max'.
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)
93 unsigned long map_size, desc_size;
94 efi_memory_desc_t *map;
96 unsigned long nr_pages;
100 status = __get_map(sys_table_arg, &map, &map_size, &desc_size);
101 if (status != EFI_SUCCESS)
104 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
106 for (i = 0; i < map_size / desc_size; i++) {
107 efi_memory_desc_t *desc;
108 unsigned long m = (unsigned long)map;
111 desc = (efi_memory_desc_t *)(m + (i * desc_size));
112 if (desc->type != EFI_CONVENTIONAL_MEMORY)
115 if (desc->num_pages < nr_pages)
118 start = desc->phys_addr;
119 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
121 if ((start + size) > end || (start + size) > max)
124 if (end - size > max)
127 if (round_down(end - size, align) < start)
130 start = round_down(end - size, align);
133 * Don't allocate at 0x0. It will confuse code that
134 * checks pointers against NULL.
139 if (start > max_addr)
144 status = EFI_NOT_FOUND;
146 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
147 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
148 nr_pages, &max_addr);
149 if (status != EFI_SUCCESS) {
159 efi_call_phys1(sys_table_arg->boottime->free_pool, map);
166 * Allocate at the lowest possible address.
168 static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
169 unsigned long size, unsigned long align,
172 unsigned long map_size, desc_size;
173 efi_memory_desc_t *map;
175 unsigned long nr_pages;
178 status = __get_map(sys_table_arg, &map, &map_size, &desc_size);
179 if (status != EFI_SUCCESS)
182 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
183 for (i = 0; i < map_size / desc_size; i++) {
184 efi_memory_desc_t *desc;
185 unsigned long m = (unsigned long)map;
188 desc = (efi_memory_desc_t *)(m + (i * desc_size));
190 if (desc->type != EFI_CONVENTIONAL_MEMORY)
193 if (desc->num_pages < nr_pages)
196 start = desc->phys_addr;
197 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
200 * Don't allocate at 0x0. It will confuse code that
201 * checks pointers against NULL. Skip the first 8
202 * bytes so we start at a nice even number.
207 start = round_up(start, align);
208 if ((start + size) > end)
211 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
212 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
214 if (status == EFI_SUCCESS) {
220 if (i == map_size / desc_size)
221 status = EFI_NOT_FOUND;
224 efi_call_phys1(sys_table_arg->boottime->free_pool, map);
229 static void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
232 unsigned long nr_pages;
234 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
235 efi_call_phys2(sys_table_arg->boottime->free_pages, addr, nr_pages);
240 * Check the cmdline for a LILO-style initrd= arguments.
242 * We only support loading an initrd from the same filesystem as the
245 static efi_status_t handle_ramdisks(efi_system_table_t *sys_table_arg,
246 efi_loaded_image_t *image,
247 struct setup_header *hdr)
249 struct initrd *initrds;
250 unsigned long initrd_addr;
251 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
253 efi_file_io_interface_t *io;
254 efi_file_handle_t *fh;
263 str = (char *)(unsigned long)hdr->cmd_line_ptr;
265 j = 0; /* See close_handles */
270 for (nr_initrds = 0; *str; nr_initrds++) {
271 str = strstr(str, "initrd=");
277 /* Skip any leading slashes */
278 while (*str == '/' || *str == '\\')
281 while (*str && *str != ' ' && *str != '\n')
288 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
290 nr_initrds * sizeof(*initrds),
292 if (status != EFI_SUCCESS) {
293 efi_printk(sys_table_arg, "Failed to alloc mem for initrds\n");
297 str = (char *)(unsigned long)hdr->cmd_line_ptr;
298 for (i = 0; i < nr_initrds; i++) {
299 struct initrd *initrd;
300 efi_file_handle_t *h;
301 efi_file_info_t *info;
302 efi_char16_t filename_16[256];
303 unsigned long info_sz;
304 efi_guid_t info_guid = EFI_FILE_INFO_ID;
308 str = strstr(str, "initrd=");
314 initrd = &initrds[i];
317 /* Skip any leading slashes */
318 while (*str == '/' || *str == '\\')
321 while (*str && *str != ' ' && *str != '\n') {
322 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
335 /* Only open the volume once. */
337 efi_boot_services_t *boottime;
339 boottime = sys_table_arg->boottime;
341 status = efi_call_phys3(boottime->handle_protocol,
342 image->device_handle, &fs_proto, &io);
343 if (status != EFI_SUCCESS) {
344 efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
348 status = efi_call_phys2(io->open_volume, io, &fh);
349 if (status != EFI_SUCCESS) {
350 efi_printk(sys_table_arg, "Failed to open volume\n");
355 status = efi_call_phys5(fh->open, fh, &h, filename_16,
356 EFI_FILE_MODE_READ, (u64)0);
357 if (status != EFI_SUCCESS) {
358 efi_printk(sys_table_arg, "Failed to open initrd file: ");
359 efi_char16_printk(sys_table_arg, filename_16);
360 efi_printk(sys_table_arg, "\n");
367 status = efi_call_phys4(h->get_info, h, &info_guid,
369 if (status != EFI_BUFFER_TOO_SMALL) {
370 efi_printk(sys_table_arg, "Failed to get initrd info size\n");
375 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
376 EFI_LOADER_DATA, info_sz, &info);
377 if (status != EFI_SUCCESS) {
378 efi_printk(sys_table_arg, "Failed to alloc mem for initrd info\n");
382 status = efi_call_phys4(h->get_info, h, &info_guid,
384 if (status == EFI_BUFFER_TOO_SMALL) {
385 efi_call_phys1(sys_table_arg->boottime->free_pool,
390 file_sz = info->file_size;
391 efi_call_phys1(sys_table_arg->boottime->free_pool, info);
393 if (status != EFI_SUCCESS) {
394 efi_printk(sys_table_arg, "Failed to get initrd info\n");
398 initrd->size = file_sz;
399 initrd_total += file_sz;
406 * Multiple initrd's need to be at consecutive
407 * addresses in memory, so allocate enough memory for
410 status = efi_high_alloc(sys_table_arg, initrd_total, 0x1000,
411 &initrd_addr, hdr->initrd_addr_max);
412 if (status != EFI_SUCCESS) {
413 efi_printk(sys_table_arg, "Failed to alloc highmem for initrds\n");
417 /* We've run out of free low memory. */
418 if (initrd_addr > hdr->initrd_addr_max) {
419 efi_printk(sys_table_arg, "We've run out of free low memory\n");
420 status = EFI_INVALID_PARAMETER;
421 goto free_initrd_total;
425 for (j = 0; j < nr_initrds; j++) {
428 size = initrds[j].size;
431 if (size > EFI_READ_CHUNK_SIZE)
432 chunksize = EFI_READ_CHUNK_SIZE;
435 status = efi_call_phys3(fh->read,
438 if (status != EFI_SUCCESS) {
439 efi_printk(sys_table_arg, "Failed to read initrd\n");
440 goto free_initrd_total;
446 efi_call_phys1(fh->close, initrds[j].handle);
451 efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
453 hdr->ramdisk_image = initrd_addr;
454 hdr->ramdisk_size = initrd_total;
459 efi_free(sys_table_arg, initrd_total, initrd_addr);
462 for (k = j; k < i; k++)
463 efi_call_phys1(fh->close, initrds[k].handle);
465 efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
467 hdr->ramdisk_image = 0;
468 hdr->ramdisk_size = 0;