blob: a25675833598c358dec976e14850cba58571803b [file] [log] [blame]
Roy Franz7721da42013-09-22 15:45:27 -07001/*
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
14struct initrd {
15 efi_file_handle_t *handle;
16 u64 size;
17};
18
19
20
21
Roy Franz876dc362013-09-22 15:45:28 -070022static void efi_char16_printk(efi_system_table_t *sys_table_arg,
23 efi_char16_t *str)
Roy Franz7721da42013-09-22 15:45:27 -070024{
25 struct efi_simple_text_output_protocol *out;
26
Roy Franz876dc362013-09-22 15:45:28 -070027 out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
Roy Franz7721da42013-09-22 15:45:27 -070028 efi_call_phys2(out->output_string, out, str);
29}
30
Roy Franz876dc362013-09-22 15:45:28 -070031static void efi_printk(efi_system_table_t *sys_table_arg, char *str)
Roy Franz7721da42013-09-22 15:45:27 -070032{
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 };
Roy Franz876dc362013-09-22 15:45:28 -070041 efi_char16_printk(sys_table_arg, nl);
Roy Franz7721da42013-09-22 15:45:27 -070042 }
43
Roy Franz876dc362013-09-22 15:45:28 -070044 efi_char16_printk(sys_table_arg, ch);
Roy Franz7721da42013-09-22 15:45:27 -070045 }
46}
47
48
Roy Franz876dc362013-09-22 15:45:28 -070049static efi_status_t __get_map(efi_system_table_t *sys_table_arg,
50 efi_memory_desc_t **map,
51 unsigned long *map_size,
Roy Franz7721da42013-09-22 15:45:27 -070052 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;
60again:
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);
Roy Franz876dc362013-09-22 15:45:28 -070066 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
Roy Franz7721da42013-09-22 15:45:27 -070067 EFI_LOADER_DATA, *map_size, (void **)&m);
68 if (status != EFI_SUCCESS)
69 goto fail;
70
Roy Franz876dc362013-09-22 15:45:28 -070071 status = efi_call_phys5(sys_table_arg->boottime->get_memory_map,
72 map_size, m, &key, desc_size, &desc_version);
Roy Franz7721da42013-09-22 15:45:27 -070073 if (status == EFI_BUFFER_TOO_SMALL) {
Roy Franz876dc362013-09-22 15:45:28 -070074 efi_call_phys1(sys_table_arg->boottime->free_pool, m);
Roy Franz7721da42013-09-22 15:45:27 -070075 goto again;
76 }
77
78 if (status != EFI_SUCCESS)
Roy Franz876dc362013-09-22 15:45:28 -070079 efi_call_phys1(sys_table_arg->boottime->free_pool, m);
Roy Franz7721da42013-09-22 15:45:27 -070080
81fail:
82 *map = m;
83 return status;
84}
85
86/*
87 * Allocate at the highest possible address that is not above 'max'.
88 */
Roy Franz40e45302013-09-22 15:45:29 -070089static efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
Roy Franz876dc362013-09-22 15:45:28 -070090 unsigned long size, unsigned long align,
91 unsigned long *addr, unsigned long max)
Roy Franz7721da42013-09-22 15:45:27 -070092{
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
Roy Franz876dc362013-09-22 15:45:28 -0700100 status = __get_map(sys_table_arg, &map, &map_size, &desc_size);
Roy Franz7721da42013-09-22 15:45:27 -0700101 if (status != EFI_SUCCESS)
102 goto fail;
103
Roy Franz38dd9c02013-09-22 15:45:30 -0700104 /*
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
Roy Franz7721da42013-09-22 15:45:27 -0700112 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
113again:
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 {
Roy Franz876dc362013-09-22 15:45:28 -0700154 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
Roy Franz7721da42013-09-22 15:45:27 -0700155 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
166free_pool:
Roy Franz876dc362013-09-22 15:45:28 -0700167 efi_call_phys1(sys_table_arg->boottime->free_pool, map);
Roy Franz7721da42013-09-22 15:45:27 -0700168
169fail:
170 return status;
171}
172
173/*
174 * Allocate at the lowest possible address.
175 */
Roy Franz40e45302013-09-22 15:45:29 -0700176static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
177 unsigned long size, unsigned long align,
Roy Franz7721da42013-09-22 15:45:27 -0700178 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
Roy Franz876dc362013-09-22 15:45:28 -0700186 status = __get_map(sys_table_arg, &map, &map_size, &desc_size);
Roy Franz7721da42013-09-22 15:45:27 -0700187 if (status != EFI_SUCCESS)
188 goto fail;
189
Roy Franz38dd9c02013-09-22 15:45:30 -0700190 /*
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
Roy Franz7721da42013-09-22 15:45:27 -0700198 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
Roy Franz876dc362013-09-22 15:45:28 -0700227 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
Roy Franz7721da42013-09-22 15:45:27 -0700228 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
239free_pool:
Roy Franz876dc362013-09-22 15:45:28 -0700240 efi_call_phys1(sys_table_arg->boottime->free_pool, map);
Roy Franz7721da42013-09-22 15:45:27 -0700241fail:
242 return status;
243}
244
Roy Franz40e45302013-09-22 15:45:29 -0700245static void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
Roy Franz876dc362013-09-22 15:45:28 -0700246 unsigned long addr)
Roy Franz7721da42013-09-22 15:45:27 -0700247{
248 unsigned long nr_pages;
249
250 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
Roy Franz876dc362013-09-22 15:45:28 -0700251 efi_call_phys2(sys_table_arg->boottime->free_pages, addr, nr_pages);
Roy Franz7721da42013-09-22 15:45:27 -0700252}
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 */
Roy Franz876dc362013-09-22 15:45:28 -0700261static efi_status_t handle_ramdisks(efi_system_table_t *sys_table_arg,
262 efi_loaded_image_t *image,
Roy Franz7721da42013-09-22 15:45:27 -0700263 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
Roy Franz876dc362013-09-22 15:45:28 -0700304 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
Roy Franz7721da42013-09-22 15:45:27 -0700305 EFI_LOADER_DATA,
306 nr_initrds * sizeof(*initrds),
307 &initrds);
308 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700309 efi_printk(sys_table_arg, "Failed to alloc mem for initrds\n");
Roy Franz7721da42013-09-22 15:45:27 -0700310 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
Roy Franz876dc362013-09-22 15:45:28 -0700355 boottime = sys_table_arg->boottime;
Roy Franz7721da42013-09-22 15:45:27 -0700356
357 status = efi_call_phys3(boottime->handle_protocol,
358 image->device_handle, &fs_proto, &io);
359 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700360 efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
Roy Franz7721da42013-09-22 15:45:27 -0700361 goto free_initrds;
362 }
363
364 status = efi_call_phys2(io->open_volume, io, &fh);
365 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700366 efi_printk(sys_table_arg, "Failed to open volume\n");
Roy Franz7721da42013-09-22 15:45:27 -0700367 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) {
Roy Franz876dc362013-09-22 15:45:28 -0700374 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");
Roy Franz7721da42013-09-22 15:45:27 -0700377 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) {
Roy Franz876dc362013-09-22 15:45:28 -0700386 efi_printk(sys_table_arg, "Failed to get initrd info size\n");
Roy Franz7721da42013-09-22 15:45:27 -0700387 goto close_handles;
388 }
389
390grow:
Roy Franz876dc362013-09-22 15:45:28 -0700391 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
Roy Franz7721da42013-09-22 15:45:27 -0700392 EFI_LOADER_DATA, info_sz, &info);
393 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700394 efi_printk(sys_table_arg, "Failed to alloc mem for initrd info\n");
Roy Franz7721da42013-09-22 15:45:27 -0700395 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) {
Roy Franz876dc362013-09-22 15:45:28 -0700401 efi_call_phys1(sys_table_arg->boottime->free_pool,
402 info);
Roy Franz7721da42013-09-22 15:45:27 -0700403 goto grow;
404 }
405
406 file_sz = info->file_size;
Roy Franz876dc362013-09-22 15:45:28 -0700407 efi_call_phys1(sys_table_arg->boottime->free_pool, info);
Roy Franz7721da42013-09-22 15:45:27 -0700408
409 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700410 efi_printk(sys_table_arg, "Failed to get initrd info\n");
Roy Franz7721da42013-09-22 15:45:27 -0700411 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 */
Roy Franz40e45302013-09-22 15:45:29 -0700426 status = efi_high_alloc(sys_table_arg, initrd_total, 0x1000,
427 &initrd_addr, hdr->initrd_addr_max);
Roy Franz7721da42013-09-22 15:45:27 -0700428 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700429 efi_printk(sys_table_arg, "Failed to alloc highmem for initrds\n");
Roy Franz7721da42013-09-22 15:45:27 -0700430 goto close_handles;
431 }
432
433 /* We've run out of free low memory. */
434 if (initrd_addr > hdr->initrd_addr_max) {
Roy Franz876dc362013-09-22 15:45:28 -0700435 efi_printk(sys_table_arg, "We've run out of free low memory\n");
Roy Franz7721da42013-09-22 15:45:27 -0700436 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) {
Roy Franz876dc362013-09-22 15:45:28 -0700455 efi_printk(sys_table_arg, "Failed to read initrd\n");
Roy Franz7721da42013-09-22 15:45:27 -0700456 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
Roy Franz876dc362013-09-22 15:45:28 -0700467 efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
Roy Franz7721da42013-09-22 15:45:27 -0700468
469 hdr->ramdisk_image = initrd_addr;
470 hdr->ramdisk_size = initrd_total;
471
472 return status;
473
474free_initrd_total:
Roy Franz40e45302013-09-22 15:45:29 -0700475 efi_free(sys_table_arg, initrd_total, initrd_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700476
477close_handles:
478 for (k = j; k < i; k++)
479 efi_call_phys1(fh->close, initrds[k].handle);
480free_initrds:
Roy Franz876dc362013-09-22 15:45:28 -0700481 efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
Roy Franz7721da42013-09-22 15:45:27 -0700482fail:
483 hdr->ramdisk_image = 0;
484 hdr->ramdisk_size = 0;
485
486 return status;
487}