blob: 05c539e9a3c856e8b62c554a8b33b8c32909a318 [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 Franz876dc362013-09-22 15:45:28 -070089static efi_status_t high_alloc(efi_system_table_t *sys_table_arg,
90 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
104 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
105again:
106 for (i = 0; i < map_size / desc_size; i++) {
107 efi_memory_desc_t *desc;
108 unsigned long m = (unsigned long)map;
109 u64 start, end;
110
111 desc = (efi_memory_desc_t *)(m + (i * desc_size));
112 if (desc->type != EFI_CONVENTIONAL_MEMORY)
113 continue;
114
115 if (desc->num_pages < nr_pages)
116 continue;
117
118 start = desc->phys_addr;
119 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
120
121 if ((start + size) > end || (start + size) > max)
122 continue;
123
124 if (end - size > max)
125 end = max;
126
127 if (round_down(end - size, align) < start)
128 continue;
129
130 start = round_down(end - size, align);
131
132 /*
133 * Don't allocate at 0x0. It will confuse code that
134 * checks pointers against NULL.
135 */
136 if (start == 0x0)
137 continue;
138
139 if (start > max_addr)
140 max_addr = start;
141 }
142
143 if (!max_addr)
144 status = EFI_NOT_FOUND;
145 else {
Roy Franz876dc362013-09-22 15:45:28 -0700146 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
Roy Franz7721da42013-09-22 15:45:27 -0700147 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
148 nr_pages, &max_addr);
149 if (status != EFI_SUCCESS) {
150 max = max_addr;
151 max_addr = 0;
152 goto again;
153 }
154
155 *addr = max_addr;
156 }
157
158free_pool:
Roy Franz876dc362013-09-22 15:45:28 -0700159 efi_call_phys1(sys_table_arg->boottime->free_pool, map);
Roy Franz7721da42013-09-22 15:45:27 -0700160
161fail:
162 return status;
163}
164
165/*
166 * Allocate at the lowest possible address.
167 */
Roy Franz876dc362013-09-22 15:45:28 -0700168static efi_status_t low_alloc(efi_system_table_t *sys_table_arg,
169 unsigned long size, unsigned long align,
Roy Franz7721da42013-09-22 15:45:27 -0700170 unsigned long *addr)
171{
172 unsigned long map_size, desc_size;
173 efi_memory_desc_t *map;
174 efi_status_t status;
175 unsigned long nr_pages;
176 int i;
177
Roy Franz876dc362013-09-22 15:45:28 -0700178 status = __get_map(sys_table_arg, &map, &map_size, &desc_size);
Roy Franz7721da42013-09-22 15:45:27 -0700179 if (status != EFI_SUCCESS)
180 goto fail;
181
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;
186 u64 start, end;
187
188 desc = (efi_memory_desc_t *)(m + (i * desc_size));
189
190 if (desc->type != EFI_CONVENTIONAL_MEMORY)
191 continue;
192
193 if (desc->num_pages < nr_pages)
194 continue;
195
196 start = desc->phys_addr;
197 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
198
199 /*
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.
203 */
204 if (start == 0x0)
205 start += 8;
206
207 start = round_up(start, align);
208 if ((start + size) > end)
209 continue;
210
Roy Franz876dc362013-09-22 15:45:28 -0700211 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
Roy Franz7721da42013-09-22 15:45:27 -0700212 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
213 nr_pages, &start);
214 if (status == EFI_SUCCESS) {
215 *addr = start;
216 break;
217 }
218 }
219
220 if (i == map_size / desc_size)
221 status = EFI_NOT_FOUND;
222
223free_pool:
Roy Franz876dc362013-09-22 15:45:28 -0700224 efi_call_phys1(sys_table_arg->boottime->free_pool, map);
Roy Franz7721da42013-09-22 15:45:27 -0700225fail:
226 return status;
227}
228
Roy Franz876dc362013-09-22 15:45:28 -0700229static void low_free(efi_system_table_t *sys_table_arg, unsigned long size,
230 unsigned long addr)
Roy Franz7721da42013-09-22 15:45:27 -0700231{
232 unsigned long nr_pages;
233
234 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
Roy Franz876dc362013-09-22 15:45:28 -0700235 efi_call_phys2(sys_table_arg->boottime->free_pages, addr, nr_pages);
Roy Franz7721da42013-09-22 15:45:27 -0700236}
237
238
239/*
240 * Check the cmdline for a LILO-style initrd= arguments.
241 *
242 * We only support loading an initrd from the same filesystem as the
243 * kernel image.
244 */
Roy Franz876dc362013-09-22 15:45:28 -0700245static efi_status_t handle_ramdisks(efi_system_table_t *sys_table_arg,
246 efi_loaded_image_t *image,
Roy Franz7721da42013-09-22 15:45:27 -0700247 struct setup_header *hdr)
248{
249 struct initrd *initrds;
250 unsigned long initrd_addr;
251 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
252 u64 initrd_total;
253 efi_file_io_interface_t *io;
254 efi_file_handle_t *fh;
255 efi_status_t status;
256 int nr_initrds;
257 char *str;
258 int i, j, k;
259
260 initrd_addr = 0;
261 initrd_total = 0;
262
263 str = (char *)(unsigned long)hdr->cmd_line_ptr;
264
265 j = 0; /* See close_handles */
266
267 if (!str || !*str)
268 return EFI_SUCCESS;
269
270 for (nr_initrds = 0; *str; nr_initrds++) {
271 str = strstr(str, "initrd=");
272 if (!str)
273 break;
274
275 str += 7;
276
277 /* Skip any leading slashes */
278 while (*str == '/' || *str == '\\')
279 str++;
280
281 while (*str && *str != ' ' && *str != '\n')
282 str++;
283 }
284
285 if (!nr_initrds)
286 return EFI_SUCCESS;
287
Roy Franz876dc362013-09-22 15:45:28 -0700288 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
Roy Franz7721da42013-09-22 15:45:27 -0700289 EFI_LOADER_DATA,
290 nr_initrds * sizeof(*initrds),
291 &initrds);
292 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700293 efi_printk(sys_table_arg, "Failed to alloc mem for initrds\n");
Roy Franz7721da42013-09-22 15:45:27 -0700294 goto fail;
295 }
296
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;
305 efi_char16_t *p;
306 u64 file_sz;
307
308 str = strstr(str, "initrd=");
309 if (!str)
310 break;
311
312 str += 7;
313
314 initrd = &initrds[i];
315 p = filename_16;
316
317 /* Skip any leading slashes */
318 while (*str == '/' || *str == '\\')
319 str++;
320
321 while (*str && *str != ' ' && *str != '\n') {
322 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
323 break;
324
325 if (*str == '/') {
326 *p++ = '\\';
327 *str++;
328 } else {
329 *p++ = *str++;
330 }
331 }
332
333 *p = '\0';
334
335 /* Only open the volume once. */
336 if (!i) {
337 efi_boot_services_t *boottime;
338
Roy Franz876dc362013-09-22 15:45:28 -0700339 boottime = sys_table_arg->boottime;
Roy Franz7721da42013-09-22 15:45:27 -0700340
341 status = efi_call_phys3(boottime->handle_protocol,
342 image->device_handle, &fs_proto, &io);
343 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700344 efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
Roy Franz7721da42013-09-22 15:45:27 -0700345 goto free_initrds;
346 }
347
348 status = efi_call_phys2(io->open_volume, io, &fh);
349 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700350 efi_printk(sys_table_arg, "Failed to open volume\n");
Roy Franz7721da42013-09-22 15:45:27 -0700351 goto free_initrds;
352 }
353 }
354
355 status = efi_call_phys5(fh->open, fh, &h, filename_16,
356 EFI_FILE_MODE_READ, (u64)0);
357 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700358 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");
Roy Franz7721da42013-09-22 15:45:27 -0700361 goto close_handles;
362 }
363
364 initrd->handle = h;
365
366 info_sz = 0;
367 status = efi_call_phys4(h->get_info, h, &info_guid,
368 &info_sz, NULL);
369 if (status != EFI_BUFFER_TOO_SMALL) {
Roy Franz876dc362013-09-22 15:45:28 -0700370 efi_printk(sys_table_arg, "Failed to get initrd info size\n");
Roy Franz7721da42013-09-22 15:45:27 -0700371 goto close_handles;
372 }
373
374grow:
Roy Franz876dc362013-09-22 15:45:28 -0700375 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
Roy Franz7721da42013-09-22 15:45:27 -0700376 EFI_LOADER_DATA, info_sz, &info);
377 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700378 efi_printk(sys_table_arg, "Failed to alloc mem for initrd info\n");
Roy Franz7721da42013-09-22 15:45:27 -0700379 goto close_handles;
380 }
381
382 status = efi_call_phys4(h->get_info, h, &info_guid,
383 &info_sz, info);
384 if (status == EFI_BUFFER_TOO_SMALL) {
Roy Franz876dc362013-09-22 15:45:28 -0700385 efi_call_phys1(sys_table_arg->boottime->free_pool,
386 info);
Roy Franz7721da42013-09-22 15:45:27 -0700387 goto grow;
388 }
389
390 file_sz = info->file_size;
Roy Franz876dc362013-09-22 15:45:28 -0700391 efi_call_phys1(sys_table_arg->boottime->free_pool, info);
Roy Franz7721da42013-09-22 15:45:27 -0700392
393 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700394 efi_printk(sys_table_arg, "Failed to get initrd info\n");
Roy Franz7721da42013-09-22 15:45:27 -0700395 goto close_handles;
396 }
397
398 initrd->size = file_sz;
399 initrd_total += file_sz;
400 }
401
402 if (initrd_total) {
403 unsigned long addr;
404
405 /*
406 * Multiple initrd's need to be at consecutive
407 * addresses in memory, so allocate enough memory for
408 * all the initrd's.
409 */
Roy Franz876dc362013-09-22 15:45:28 -0700410 status = high_alloc(sys_table_arg, initrd_total, 0x1000,
Roy Franz7721da42013-09-22 15:45:27 -0700411 &initrd_addr, hdr->initrd_addr_max);
412 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700413 efi_printk(sys_table_arg, "Failed to alloc highmem for initrds\n");
Roy Franz7721da42013-09-22 15:45:27 -0700414 goto close_handles;
415 }
416
417 /* We've run out of free low memory. */
418 if (initrd_addr > hdr->initrd_addr_max) {
Roy Franz876dc362013-09-22 15:45:28 -0700419 efi_printk(sys_table_arg, "We've run out of free low memory\n");
Roy Franz7721da42013-09-22 15:45:27 -0700420 status = EFI_INVALID_PARAMETER;
421 goto free_initrd_total;
422 }
423
424 addr = initrd_addr;
425 for (j = 0; j < nr_initrds; j++) {
426 u64 size;
427
428 size = initrds[j].size;
429 while (size) {
430 u64 chunksize;
431 if (size > EFI_READ_CHUNK_SIZE)
432 chunksize = EFI_READ_CHUNK_SIZE;
433 else
434 chunksize = size;
435 status = efi_call_phys3(fh->read,
436 initrds[j].handle,
437 &chunksize, addr);
438 if (status != EFI_SUCCESS) {
Roy Franz876dc362013-09-22 15:45:28 -0700439 efi_printk(sys_table_arg, "Failed to read initrd\n");
Roy Franz7721da42013-09-22 15:45:27 -0700440 goto free_initrd_total;
441 }
442 addr += chunksize;
443 size -= chunksize;
444 }
445
446 efi_call_phys1(fh->close, initrds[j].handle);
447 }
448
449 }
450
Roy Franz876dc362013-09-22 15:45:28 -0700451 efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
Roy Franz7721da42013-09-22 15:45:27 -0700452
453 hdr->ramdisk_image = initrd_addr;
454 hdr->ramdisk_size = initrd_total;
455
456 return status;
457
458free_initrd_total:
Roy Franz876dc362013-09-22 15:45:28 -0700459 low_free(sys_table_arg, initrd_total, initrd_addr);
Roy Franz7721da42013-09-22 15:45:27 -0700460
461close_handles:
462 for (k = j; k < i; k++)
463 efi_call_phys1(fh->close, initrds[k].handle);
464free_initrds:
Roy Franz876dc362013-09-22 15:45:28 -0700465 efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
Roy Franz7721da42013-09-22 15:45:27 -0700466fail:
467 hdr->ramdisk_image = 0;
468 hdr->ramdisk_size = 0;
469
470 return status;
471}