blob: f8fa41190c3526f61ec7bb6e1dc5b0ebf08b3c4d [file] [log] [blame]
Matt Fleming291f3632011-12-12 21:27:52 +00001/* -----------------------------------------------------------------------
2 *
3 * Copyright 2011 Intel Corporation; author Matt Fleming
4 *
5 * This file is part of the Linux kernel, and is made available under
6 * the terms of the GNU General Public License version 2.
7 *
8 * ----------------------------------------------------------------------- */
9
10#include <linux/efi.h>
Matthew Garrettdd5fc852012-12-05 14:33:26 -070011#include <linux/pci.h>
Matt Fleming291f3632011-12-12 21:27:52 +000012#include <asm/efi.h>
13#include <asm/setup.h>
14#include <asm/desc.h>
15
Matt Fleming0f905a42012-11-20 13:07:46 +000016#undef memcpy /* Use memcpy from misc.c */
17
Matt Fleming291f3632011-12-12 21:27:52 +000018#include "eboot.h"
19
20static efi_system_table_t *sys_table;
21
Matt Fleming9fa7ded2012-02-20 13:20:59 +000022static void efi_printk(char *str)
23{
24 char *s8;
25
26 for (s8 = str; *s8; s8++) {
27 struct efi_simple_text_output_protocol *out;
28 efi_char16_t ch[2] = { 0 };
29
30 ch[0] = *s8;
31 out = (struct efi_simple_text_output_protocol *)sys_table->con_out;
32
33 if (*s8 == '\n') {
34 efi_char16_t nl[2] = { '\r', 0 };
35 efi_call_phys2(out->output_string, out, nl);
36 }
37
38 efi_call_phys2(out->output_string, out, ch);
39 }
40}
41
Matt Fleming291f3632011-12-12 21:27:52 +000042static efi_status_t __get_map(efi_memory_desc_t **map, unsigned long *map_size,
43 unsigned long *desc_size)
44{
45 efi_memory_desc_t *m = NULL;
46 efi_status_t status;
47 unsigned long key;
48 u32 desc_version;
49
50 *map_size = sizeof(*m) * 32;
51again:
52 /*
53 * Add an additional efi_memory_desc_t because we're doing an
54 * allocation which may be in a new descriptor region.
55 */
56 *map_size += sizeof(*m);
57 status = efi_call_phys3(sys_table->boottime->allocate_pool,
58 EFI_LOADER_DATA, *map_size, (void **)&m);
59 if (status != EFI_SUCCESS)
60 goto fail;
61
62 status = efi_call_phys5(sys_table->boottime->get_memory_map, map_size,
63 m, &key, desc_size, &desc_version);
64 if (status == EFI_BUFFER_TOO_SMALL) {
65 efi_call_phys1(sys_table->boottime->free_pool, m);
66 goto again;
67 }
68
69 if (status != EFI_SUCCESS)
70 efi_call_phys1(sys_table->boottime->free_pool, m);
71
72fail:
73 *map = m;
74 return status;
75}
76
77/*
78 * Allocate at the highest possible address that is not above 'max'.
79 */
80static efi_status_t high_alloc(unsigned long size, unsigned long align,
81 unsigned long *addr, unsigned long max)
82{
83 unsigned long map_size, desc_size;
84 efi_memory_desc_t *map;
85 efi_status_t status;
86 unsigned long nr_pages;
87 u64 max_addr = 0;
88 int i;
89
90 status = __get_map(&map, &map_size, &desc_size);
91 if (status != EFI_SUCCESS)
92 goto fail;
93
94 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
95again:
96 for (i = 0; i < map_size / desc_size; i++) {
97 efi_memory_desc_t *desc;
98 unsigned long m = (unsigned long)map;
99 u64 start, end;
100
101 desc = (efi_memory_desc_t *)(m + (i * desc_size));
102 if (desc->type != EFI_CONVENTIONAL_MEMORY)
103 continue;
104
105 if (desc->num_pages < nr_pages)
106 continue;
107
108 start = desc->phys_addr;
109 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
110
111 if ((start + size) > end || (start + size) > max)
112 continue;
113
114 if (end - size > max)
115 end = max;
116
117 if (round_down(end - size, align) < start)
118 continue;
119
120 start = round_down(end - size, align);
121
122 /*
123 * Don't allocate at 0x0. It will confuse code that
124 * checks pointers against NULL.
125 */
126 if (start == 0x0)
127 continue;
128
129 if (start > max_addr)
130 max_addr = start;
131 }
132
133 if (!max_addr)
134 status = EFI_NOT_FOUND;
135 else {
136 status = efi_call_phys4(sys_table->boottime->allocate_pages,
137 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
138 nr_pages, &max_addr);
139 if (status != EFI_SUCCESS) {
140 max = max_addr;
141 max_addr = 0;
142 goto again;
143 }
144
145 *addr = max_addr;
146 }
147
148free_pool:
149 efi_call_phys1(sys_table->boottime->free_pool, map);
150
151fail:
152 return status;
153}
154
155/*
156 * Allocate at the lowest possible address.
157 */
158static efi_status_t low_alloc(unsigned long size, unsigned long align,
159 unsigned long *addr)
160{
161 unsigned long map_size, desc_size;
162 efi_memory_desc_t *map;
163 efi_status_t status;
164 unsigned long nr_pages;
165 int i;
166
167 status = __get_map(&map, &map_size, &desc_size);
168 if (status != EFI_SUCCESS)
169 goto fail;
170
171 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
172 for (i = 0; i < map_size / desc_size; i++) {
173 efi_memory_desc_t *desc;
174 unsigned long m = (unsigned long)map;
175 u64 start, end;
176
177 desc = (efi_memory_desc_t *)(m + (i * desc_size));
178
179 if (desc->type != EFI_CONVENTIONAL_MEMORY)
180 continue;
181
182 if (desc->num_pages < nr_pages)
183 continue;
184
185 start = desc->phys_addr;
186 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
187
188 /*
189 * Don't allocate at 0x0. It will confuse code that
190 * checks pointers against NULL. Skip the first 8
191 * bytes so we start at a nice even number.
192 */
193 if (start == 0x0)
194 start += 8;
195
196 start = round_up(start, align);
197 if ((start + size) > end)
198 continue;
199
200 status = efi_call_phys4(sys_table->boottime->allocate_pages,
201 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
202 nr_pages, &start);
203 if (status == EFI_SUCCESS) {
204 *addr = start;
205 break;
206 }
207 }
208
209 if (i == map_size / desc_size)
210 status = EFI_NOT_FOUND;
211
212free_pool:
213 efi_call_phys1(sys_table->boottime->free_pool, map);
214fail:
215 return status;
216}
217
218static void low_free(unsigned long size, unsigned long addr)
219{
220 unsigned long nr_pages;
221
222 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
223 efi_call_phys2(sys_table->boottime->free_pages, addr, size);
224}
225
226static void find_bits(unsigned long mask, u8 *pos, u8 *size)
227{
228 u8 first, len;
229
230 first = 0;
231 len = 0;
232
233 if (mask) {
234 while (!(mask & 0x1)) {
235 mask = mask >> 1;
236 first++;
237 }
238
239 while (mask & 0x1) {
240 mask = mask >> 1;
241 len++;
242 }
243 }
244
245 *pos = first;
246 *size = len;
247}
248
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700249static efi_status_t setup_efi_pci(struct boot_params *params)
250{
251 efi_pci_io_protocol *pci;
252 efi_status_t status;
253 void **pci_handle;
254 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
255 unsigned long nr_pci, size = 0;
256 int i;
257 struct setup_data *data;
258
Jan Beulichbc754792013-01-18 12:35:14 +0000259 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700260
261 while (data && data->next)
Jan Beulichbc754792013-01-18 12:35:14 +0000262 data = (struct setup_data *)(unsigned long)data->next;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700263
264 status = efi_call_phys5(sys_table->boottime->locate_handle,
265 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
266 NULL, &size, pci_handle);
267
268 if (status == EFI_BUFFER_TOO_SMALL) {
269 status = efi_call_phys3(sys_table->boottime->allocate_pool,
270 EFI_LOADER_DATA, size, &pci_handle);
271
272 if (status != EFI_SUCCESS)
273 return status;
274
275 status = efi_call_phys5(sys_table->boottime->locate_handle,
276 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
277 NULL, &size, pci_handle);
278 }
279
280 if (status != EFI_SUCCESS)
281 goto free_handle;
282
283 nr_pci = size / sizeof(void *);
284 for (i = 0; i < nr_pci; i++) {
285 void *h = pci_handle[i];
286 uint64_t attributes;
287 struct pci_setup_rom *rom;
288
289 status = efi_call_phys3(sys_table->boottime->handle_protocol,
290 h, &pci_proto, &pci);
291
292 if (status != EFI_SUCCESS)
293 continue;
294
295 if (!pci)
296 continue;
297
David Woodhouseb607e212013-01-07 22:09:49 +0000298#ifdef CONFIG_X86_64
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700299 status = efi_call_phys4(pci->attributes, pci,
300 EfiPciIoAttributeOperationGet, 0,
301 &attributes);
David Woodhouseb607e212013-01-07 22:09:49 +0000302#else
303 status = efi_call_phys5(pci->attributes, pci,
304 EfiPciIoAttributeOperationGet, 0, 0,
305 &attributes);
306#endif
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700307 if (status != EFI_SUCCESS)
308 continue;
309
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700310 if (!pci->romimage || !pci->romsize)
311 continue;
312
313 size = pci->romsize + sizeof(*rom);
314
315 status = efi_call_phys3(sys_table->boottime->allocate_pool,
316 EFI_LOADER_DATA, size, &rom);
317
318 if (status != EFI_SUCCESS)
319 continue;
320
321 rom->data.type = SETUP_PCI;
322 rom->data.len = size - sizeof(struct setup_data);
323 rom->data.next = 0;
324 rom->pcilen = pci->romsize;
325
326 status = efi_call_phys5(pci->pci.read, pci,
327 EfiPciIoWidthUint16, PCI_VENDOR_ID,
328 1, &(rom->vendor));
329
330 if (status != EFI_SUCCESS)
331 goto free_struct;
332
333 status = efi_call_phys5(pci->pci.read, pci,
334 EfiPciIoWidthUint16, PCI_DEVICE_ID,
335 1, &(rom->devid));
336
337 if (status != EFI_SUCCESS)
338 goto free_struct;
339
340 status = efi_call_phys5(pci->get_location, pci,
341 &(rom->segment), &(rom->bus),
342 &(rom->device), &(rom->function));
343
344 if (status != EFI_SUCCESS)
345 goto free_struct;
346
347 memcpy(rom->romdata, pci->romimage, pci->romsize);
348
349 if (data)
Jan Beulichbc754792013-01-18 12:35:14 +0000350 data->next = (unsigned long)rom;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700351 else
Jan Beulichbc754792013-01-18 12:35:14 +0000352 params->hdr.setup_data = (unsigned long)rom;
Matthew Garrettdd5fc852012-12-05 14:33:26 -0700353
354 data = (struct setup_data *)rom;
355
356 continue;
357 free_struct:
358 efi_call_phys1(sys_table->boottime->free_pool, rom);
359 }
360
361free_handle:
362 efi_call_phys1(sys_table->boottime->free_pool, pci_handle);
363 return status;
364}
365
Matt Fleming291f3632011-12-12 21:27:52 +0000366/*
367 * See if we have Graphics Output Protocol
368 */
369static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
370 unsigned long size)
371{
372 struct efi_graphics_output_protocol *gop, *first_gop;
373 struct efi_pixel_bitmask pixel_info;
374 unsigned long nr_gops;
375 efi_status_t status;
376 void **gop_handle;
377 u16 width, height;
378 u32 fb_base, fb_size;
379 u32 pixels_per_scan_line;
380 int pixel_format;
381 int i;
382
383 status = efi_call_phys3(sys_table->boottime->allocate_pool,
384 EFI_LOADER_DATA, size, &gop_handle);
385 if (status != EFI_SUCCESS)
386 return status;
387
388 status = efi_call_phys5(sys_table->boottime->locate_handle,
389 EFI_LOCATE_BY_PROTOCOL, proto,
390 NULL, &size, gop_handle);
391 if (status != EFI_SUCCESS)
392 goto free_handle;
393
394 first_gop = NULL;
395
396 nr_gops = size / sizeof(void *);
397 for (i = 0; i < nr_gops; i++) {
398 struct efi_graphics_output_mode_info *info;
Matthew Garrett38cb5ef2012-07-26 18:00:27 -0400399 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
400 bool conout_found = false;
401 void *dummy;
Matt Fleming291f3632011-12-12 21:27:52 +0000402 void *h = gop_handle[i];
403
404 status = efi_call_phys3(sys_table->boottime->handle_protocol,
405 h, proto, &gop);
406 if (status != EFI_SUCCESS)
407 continue;
408
Matthew Garrett38cb5ef2012-07-26 18:00:27 -0400409 status = efi_call_phys3(sys_table->boottime->handle_protocol,
410 h, &conout_proto, &dummy);
411
412 if (status == EFI_SUCCESS)
413 conout_found = true;
Matt Fleming291f3632011-12-12 21:27:52 +0000414
415 status = efi_call_phys4(gop->query_mode, gop,
416 gop->mode->mode, &size, &info);
Matthew Garrett38cb5ef2012-07-26 18:00:27 -0400417 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
Matt Fleming291f3632011-12-12 21:27:52 +0000418 /*
Matthew Garrett38cb5ef2012-07-26 18:00:27 -0400419 * Systems that use the UEFI Console Splitter may
420 * provide multiple GOP devices, not all of which are
421 * backed by real hardware. The workaround is to search
422 * for a GOP implementing the ConOut protocol, and if
423 * one isn't found, to just fall back to the first GOP.
Matt Fleming291f3632011-12-12 21:27:52 +0000424 */
425 width = info->horizontal_resolution;
426 height = info->vertical_resolution;
427 fb_base = gop->mode->frame_buffer_base;
428 fb_size = gop->mode->frame_buffer_size;
429 pixel_format = info->pixel_format;
430 pixel_info = info->pixel_information;
431 pixels_per_scan_line = info->pixels_per_scan_line;
432
433 /*
Matthew Garrett38cb5ef2012-07-26 18:00:27 -0400434 * Once we've found a GOP supporting ConOut,
Matt Fleming291f3632011-12-12 21:27:52 +0000435 * don't bother looking any further.
436 */
David Woodhouse70a479c2013-01-07 21:52:16 +0000437 first_gop = gop;
Matthew Garrett38cb5ef2012-07-26 18:00:27 -0400438 if (conout_found)
Matt Fleming291f3632011-12-12 21:27:52 +0000439 break;
Matt Fleming291f3632011-12-12 21:27:52 +0000440 }
441 }
442
443 /* Did we find any GOPs? */
444 if (!first_gop)
445 goto free_handle;
446
447 /* EFI framebuffer */
448 si->orig_video_isVGA = VIDEO_TYPE_EFI;
449
450 si->lfb_width = width;
451 si->lfb_height = height;
452 si->lfb_base = fb_base;
Matt Fleming291f3632011-12-12 21:27:52 +0000453 si->pages = 1;
454
455 if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
456 si->lfb_depth = 32;
457 si->lfb_linelength = pixels_per_scan_line * 4;
458 si->red_size = 8;
459 si->red_pos = 0;
460 si->green_size = 8;
461 si->green_pos = 8;
462 si->blue_size = 8;
463 si->blue_pos = 16;
464 si->rsvd_size = 8;
465 si->rsvd_pos = 24;
466 } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) {
467 si->lfb_depth = 32;
468 si->lfb_linelength = pixels_per_scan_line * 4;
469 si->red_size = 8;
470 si->red_pos = 16;
471 si->green_size = 8;
472 si->green_pos = 8;
473 si->blue_size = 8;
474 si->blue_pos = 0;
475 si->rsvd_size = 8;
476 si->rsvd_pos = 24;
477 } else if (pixel_format == PIXEL_BIT_MASK) {
478 find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
479 find_bits(pixel_info.green_mask, &si->green_pos,
480 &si->green_size);
481 find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
482 find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
483 &si->rsvd_size);
484 si->lfb_depth = si->red_size + si->green_size +
485 si->blue_size + si->rsvd_size;
486 si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
487 } else {
488 si->lfb_depth = 4;
489 si->lfb_linelength = si->lfb_width / 2;
490 si->red_size = 0;
491 si->red_pos = 0;
492 si->green_size = 0;
493 si->green_pos = 0;
494 si->blue_size = 0;
495 si->blue_pos = 0;
496 si->rsvd_size = 0;
497 si->rsvd_pos = 0;
498 }
499
Matthew Garrette9b10952012-07-27 17:20:49 -0400500 si->lfb_size = si->lfb_linelength * si->lfb_height;
501
Matthew Garrettf462ed92012-07-27 12:58:53 -0400502 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
503
Matt Fleming291f3632011-12-12 21:27:52 +0000504free_handle:
505 efi_call_phys1(sys_table->boottime->free_pool, gop_handle);
506 return status;
507}
508
509/*
510 * See if we have Universal Graphics Adapter (UGA) protocol
511 */
512static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
513 unsigned long size)
514{
515 struct efi_uga_draw_protocol *uga, *first_uga;
516 unsigned long nr_ugas;
517 efi_status_t status;
518 u32 width, height;
519 void **uga_handle = NULL;
520 int i;
521
522 status = efi_call_phys3(sys_table->boottime->allocate_pool,
523 EFI_LOADER_DATA, size, &uga_handle);
524 if (status != EFI_SUCCESS)
525 return status;
526
527 status = efi_call_phys5(sys_table->boottime->locate_handle,
528 EFI_LOCATE_BY_PROTOCOL, uga_proto,
529 NULL, &size, uga_handle);
530 if (status != EFI_SUCCESS)
531 goto free_handle;
532
533 first_uga = NULL;
534
535 nr_ugas = size / sizeof(void *);
536 for (i = 0; i < nr_ugas; i++) {
537 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
538 void *handle = uga_handle[i];
539 u32 w, h, depth, refresh;
540 void *pciio;
541
542 status = efi_call_phys3(sys_table->boottime->handle_protocol,
543 handle, uga_proto, &uga);
544 if (status != EFI_SUCCESS)
545 continue;
546
547 efi_call_phys3(sys_table->boottime->handle_protocol,
548 handle, &pciio_proto, &pciio);
549
550 status = efi_call_phys5(uga->get_mode, uga, &w, &h,
551 &depth, &refresh);
552 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
553 width = w;
554 height = h;
555
556 /*
557 * Once we've found a UGA supporting PCIIO,
558 * don't bother looking any further.
559 */
560 if (pciio)
561 break;
562
563 first_uga = uga;
564 }
565 }
566
567 if (!first_uga)
568 goto free_handle;
569
570 /* EFI framebuffer */
571 si->orig_video_isVGA = VIDEO_TYPE_EFI;
572
573 si->lfb_depth = 32;
574 si->lfb_width = width;
575 si->lfb_height = height;
576
577 si->red_size = 8;
578 si->red_pos = 16;
579 si->green_size = 8;
580 si->green_pos = 8;
581 si->blue_size = 8;
582 si->blue_pos = 0;
583 si->rsvd_size = 8;
584 si->rsvd_pos = 24;
585
586
587free_handle:
588 efi_call_phys1(sys_table->boottime->free_pool, uga_handle);
589 return status;
590}
591
592void setup_graphics(struct boot_params *boot_params)
593{
594 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
595 struct screen_info *si;
596 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
597 efi_status_t status;
598 unsigned long size;
599 void **gop_handle = NULL;
600 void **uga_handle = NULL;
601
602 si = &boot_params->screen_info;
603 memset(si, 0, sizeof(*si));
604
605 size = 0;
606 status = efi_call_phys5(sys_table->boottime->locate_handle,
607 EFI_LOCATE_BY_PROTOCOL, &graphics_proto,
608 NULL, &size, gop_handle);
609 if (status == EFI_BUFFER_TOO_SMALL)
610 status = setup_gop(si, &graphics_proto, size);
611
612 if (status != EFI_SUCCESS) {
613 size = 0;
614 status = efi_call_phys5(sys_table->boottime->locate_handle,
615 EFI_LOCATE_BY_PROTOCOL, &uga_proto,
616 NULL, &size, uga_handle);
617 if (status == EFI_BUFFER_TOO_SMALL)
618 setup_uga(si, &uga_proto, size);
619 }
620}
621
622struct initrd {
623 efi_file_handle_t *handle;
624 u64 size;
625};
626
627/*
628 * Check the cmdline for a LILO-style initrd= arguments.
629 *
630 * We only support loading an initrd from the same filesystem as the
631 * kernel image.
632 */
633static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
634 struct setup_header *hdr)
635{
636 struct initrd *initrds;
637 unsigned long initrd_addr;
638 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
639 u64 initrd_total;
640 efi_file_io_interface_t *io;
641 efi_file_handle_t *fh;
642 efi_status_t status;
643 int nr_initrds;
644 char *str;
645 int i, j, k;
646
647 initrd_addr = 0;
648 initrd_total = 0;
649
650 str = (char *)(unsigned long)hdr->cmd_line_ptr;
651
652 j = 0; /* See close_handles */
653
654 if (!str || !*str)
655 return EFI_SUCCESS;
656
657 for (nr_initrds = 0; *str; nr_initrds++) {
658 str = strstr(str, "initrd=");
659 if (!str)
660 break;
661
662 str += 7;
663
664 /* Skip any leading slashes */
665 while (*str == '/' || *str == '\\')
666 str++;
667
668 while (*str && *str != ' ' && *str != '\n')
669 str++;
670 }
671
672 if (!nr_initrds)
673 return EFI_SUCCESS;
674
675 status = efi_call_phys3(sys_table->boottime->allocate_pool,
676 EFI_LOADER_DATA,
677 nr_initrds * sizeof(*initrds),
678 &initrds);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000679 if (status != EFI_SUCCESS) {
680 efi_printk("Failed to alloc mem for initrds\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000681 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000682 }
Matt Fleming291f3632011-12-12 21:27:52 +0000683
684 str = (char *)(unsigned long)hdr->cmd_line_ptr;
685 for (i = 0; i < nr_initrds; i++) {
686 struct initrd *initrd;
687 efi_file_handle_t *h;
688 efi_file_info_t *info;
Dan Carpenterc7b73832012-03-05 21:06:14 +0300689 efi_char16_t filename_16[256];
Matt Fleming291f3632011-12-12 21:27:52 +0000690 unsigned long info_sz;
691 efi_guid_t info_guid = EFI_FILE_INFO_ID;
692 efi_char16_t *p;
693 u64 file_sz;
694
695 str = strstr(str, "initrd=");
696 if (!str)
697 break;
698
699 str += 7;
700
701 initrd = &initrds[i];
Dan Carpenterc7b73832012-03-05 21:06:14 +0300702 p = filename_16;
Matt Fleming291f3632011-12-12 21:27:52 +0000703
704 /* Skip any leading slashes */
705 while (*str == '/' || *str == '\\')
706 str++;
707
708 while (*str && *str != ' ' && *str != '\n') {
Dan Carpenterc7b73832012-03-05 21:06:14 +0300709 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
Matt Fleming291f3632011-12-12 21:27:52 +0000710 break;
711
712 *p++ = *str++;
713 }
714
715 *p = '\0';
716
717 /* Only open the volume once. */
718 if (!i) {
719 efi_boot_services_t *boottime;
720
721 boottime = sys_table->boottime;
722
723 status = efi_call_phys3(boottime->handle_protocol,
724 image->device_handle, &fs_proto, &io);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000725 if (status != EFI_SUCCESS) {
726 efi_printk("Failed to handle fs_proto\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000727 goto free_initrds;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000728 }
Matt Fleming291f3632011-12-12 21:27:52 +0000729
730 status = efi_call_phys2(io->open_volume, io, &fh);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000731 if (status != EFI_SUCCESS) {
732 efi_printk("Failed to open volume\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000733 goto free_initrds;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000734 }
Matt Fleming291f3632011-12-12 21:27:52 +0000735 }
736
Dan Carpenterc7b73832012-03-05 21:06:14 +0300737 status = efi_call_phys5(fh->open, fh, &h, filename_16,
Matt Fleming291f3632011-12-12 21:27:52 +0000738 EFI_FILE_MODE_READ, (u64)0);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000739 if (status != EFI_SUCCESS) {
740 efi_printk("Failed to open initrd file\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000741 goto close_handles;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000742 }
Matt Fleming291f3632011-12-12 21:27:52 +0000743
744 initrd->handle = h;
745
746 info_sz = 0;
747 status = efi_call_phys4(h->get_info, h, &info_guid,
748 &info_sz, NULL);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000749 if (status != EFI_BUFFER_TOO_SMALL) {
750 efi_printk("Failed to get initrd info size\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000751 goto close_handles;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000752 }
Matt Fleming291f3632011-12-12 21:27:52 +0000753
754grow:
755 status = efi_call_phys3(sys_table->boottime->allocate_pool,
756 EFI_LOADER_DATA, info_sz, &info);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000757 if (status != EFI_SUCCESS) {
758 efi_printk("Failed to alloc mem for initrd info\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000759 goto close_handles;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000760 }
Matt Fleming291f3632011-12-12 21:27:52 +0000761
762 status = efi_call_phys4(h->get_info, h, &info_guid,
763 &info_sz, info);
764 if (status == EFI_BUFFER_TOO_SMALL) {
765 efi_call_phys1(sys_table->boottime->free_pool, info);
766 goto grow;
767 }
768
769 file_sz = info->file_size;
770 efi_call_phys1(sys_table->boottime->free_pool, info);
771
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000772 if (status != EFI_SUCCESS) {
773 efi_printk("Failed to get initrd info\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000774 goto close_handles;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000775 }
Matt Fleming291f3632011-12-12 21:27:52 +0000776
777 initrd->size = file_sz;
778 initrd_total += file_sz;
779 }
780
781 if (initrd_total) {
782 unsigned long addr;
783
784 /*
785 * Multiple initrd's need to be at consecutive
786 * addresses in memory, so allocate enough memory for
787 * all the initrd's.
788 */
789 status = high_alloc(initrd_total, 0x1000,
790 &initrd_addr, hdr->initrd_addr_max);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000791 if (status != EFI_SUCCESS) {
792 efi_printk("Failed to alloc highmem for initrds\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000793 goto close_handles;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000794 }
Matt Fleming291f3632011-12-12 21:27:52 +0000795
796 /* We've run out of free low memory. */
797 if (initrd_addr > hdr->initrd_addr_max) {
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000798 efi_printk("We've run out of free low memory\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000799 status = EFI_INVALID_PARAMETER;
800 goto free_initrd_total;
801 }
802
803 addr = initrd_addr;
804 for (j = 0; j < nr_initrds; j++) {
805 u64 size;
806
807 size = initrds[j].size;
Maarten Lankhorst2d2da60f2011-12-16 13:30:58 +0100808 while (size) {
809 u64 chunksize;
810 if (size > EFI_READ_CHUNK_SIZE)
811 chunksize = EFI_READ_CHUNK_SIZE;
812 else
813 chunksize = size;
814 status = efi_call_phys3(fh->read,
815 initrds[j].handle,
816 &chunksize, addr);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000817 if (status != EFI_SUCCESS) {
818 efi_printk("Failed to read initrd\n");
Maarten Lankhorst2d2da60f2011-12-16 13:30:58 +0100819 goto free_initrd_total;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000820 }
Maarten Lankhorst2d2da60f2011-12-16 13:30:58 +0100821 addr += chunksize;
822 size -= chunksize;
823 }
Matt Fleming291f3632011-12-12 21:27:52 +0000824
825 efi_call_phys1(fh->close, initrds[j].handle);
Matt Fleming291f3632011-12-12 21:27:52 +0000826 }
827
828 }
829
830 efi_call_phys1(sys_table->boottime->free_pool, initrds);
831
832 hdr->ramdisk_image = initrd_addr;
833 hdr->ramdisk_size = initrd_total;
834
835 return status;
836
837free_initrd_total:
838 low_free(initrd_total, initrd_addr);
839
840close_handles:
Matt Fleming30dc0d02012-03-15 19:13:25 +0000841 for (k = j; k < i; k++)
Matt Fleming291f3632011-12-12 21:27:52 +0000842 efi_call_phys1(fh->close, initrds[k].handle);
843free_initrds:
844 efi_call_phys1(sys_table->boottime->free_pool, initrds);
845fail:
846 hdr->ramdisk_image = 0;
847 hdr->ramdisk_size = 0;
848
849 return status;
850}
851
852/*
853 * Because the x86 boot code expects to be passed a boot_params we
854 * need to create one ourselves (usually the bootloader would create
855 * one for us).
856 */
Matt Fleming9ca8f722012-07-19 10:23:48 +0100857struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
Matt Fleming291f3632011-12-12 21:27:52 +0000858{
Matt Fleming9ca8f722012-07-19 10:23:48 +0100859 struct boot_params *boot_params;
860 struct sys_desc_table *sdt;
861 struct apm_bios_info *bi;
862 struct setup_header *hdr;
863 struct efi_info *efi;
864 efi_loaded_image_t *image;
865 void *options;
866 u32 load_options_size;
867 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
Matt Fleming291f3632011-12-12 21:27:52 +0000868 int options_size = 0;
869 efi_status_t status;
Matt Fleming291f3632011-12-12 21:27:52 +0000870 unsigned long cmdline;
Matt Fleming291f3632011-12-12 21:27:52 +0000871 u16 *s2;
872 u8 *s1;
873 int i;
874
Matt Fleming9ca8f722012-07-19 10:23:48 +0100875 sys_table = _table;
876
877 /* Check if we were booted by the EFI firmware */
878 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
879 return NULL;
880
881 status = efi_call_phys3(sys_table->boottime->handle_protocol,
882 handle, &proto, (void *)&image);
883 if (status != EFI_SUCCESS) {
884 efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
885 return NULL;
886 }
887
888 status = low_alloc(0x4000, 1, (unsigned long *)&boot_params);
889 if (status != EFI_SUCCESS) {
890 efi_printk("Failed to alloc lowmem for boot params\n");
891 return NULL;
892 }
893
894 memset(boot_params, 0x0, 0x4000);
895
896 hdr = &boot_params->hdr;
897 efi = &boot_params->efi_info;
898 bi = &boot_params->apm_bios_info;
899 sdt = &boot_params->sys_desc_table;
900
901 /* Copy the second sector to boot_params */
902 memcpy(&hdr->jump, image->image_base + 512, 512);
903
904 /*
905 * Fill out some of the header fields ourselves because the
906 * EFI firmware loader doesn't load the first sector.
907 */
908 hdr->root_flags = 1;
909 hdr->vid_mode = 0xffff;
910 hdr->boot_flag = 0xAA55;
911
912 hdr->code32_start = (__u64)(unsigned long)image->image_base;
913
Matt Fleming291f3632011-12-12 21:27:52 +0000914 hdr->type_of_loader = 0x21;
915
916 /* Convert unicode cmdline to ascii */
Matt Fleming9ca8f722012-07-19 10:23:48 +0100917 options = image->load_options;
918 load_options_size = image->load_options_size / 2; /* ASCII */
Matt Fleming291f3632011-12-12 21:27:52 +0000919 cmdline = 0;
920 s2 = (u16 *)options;
921
922 if (s2) {
923 while (*s2 && *s2 != '\n' && options_size < load_options_size) {
924 s2++;
925 options_size++;
926 }
927
928 if (options_size) {
929 if (options_size > hdr->cmdline_size)
930 options_size = hdr->cmdline_size;
931
932 options_size++; /* NUL termination */
933
934 status = low_alloc(options_size, 1, &cmdline);
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000935 if (status != EFI_SUCCESS) {
936 efi_printk("Failed to alloc mem for cmdline\n");
Matt Fleming291f3632011-12-12 21:27:52 +0000937 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +0000938 }
Matt Fleming291f3632011-12-12 21:27:52 +0000939
940 s1 = (u8 *)(unsigned long)cmdline;
941 s2 = (u16 *)options;
942
943 for (i = 0; i < options_size - 1; i++)
944 *s1++ = *s2++;
945
946 *s1 = '\0';
947 }
948 }
949
950 hdr->cmd_line_ptr = cmdline;
951
952 hdr->ramdisk_image = 0;
953 hdr->ramdisk_size = 0;
954
Matt Fleming291f3632011-12-12 21:27:52 +0000955 /* Clear APM BIOS info */
956 memset(bi, 0, sizeof(*bi));
957
958 memset(sdt, 0, sizeof(*sdt));
959
Matt Fleming9ca8f722012-07-19 10:23:48 +0100960 status = handle_ramdisks(image, hdr);
961 if (status != EFI_SUCCESS)
962 goto fail2;
963
964 return boot_params;
965fail2:
966 if (options_size)
967 low_free(options_size, hdr->cmd_line_ptr);
968fail:
969 low_free(0x4000, (unsigned long)boot_params);
970 return NULL;
971}
972
973static efi_status_t exit_boot(struct boot_params *boot_params,
974 void *handle)
975{
976 struct efi_info *efi = &boot_params->efi_info;
977 struct e820entry *e820_map = &boot_params->e820_map[0];
978 struct e820entry *prev = NULL;
979 unsigned long size, key, desc_size, _size;
980 efi_memory_desc_t *mem_map;
981 efi_status_t status;
982 __u32 desc_version;
983 u8 nr_entries;
984 int i;
Matt Fleming291f3632011-12-12 21:27:52 +0000985
986 size = sizeof(*mem_map) * 32;
987
988again:
989 size += sizeof(*mem_map);
990 _size = size;
991 status = low_alloc(size, 1, (unsigned long *)&mem_map);
992 if (status != EFI_SUCCESS)
Matt Fleming9ca8f722012-07-19 10:23:48 +0100993 return status;
Matt Fleming291f3632011-12-12 21:27:52 +0000994
995 status = efi_call_phys5(sys_table->boottime->get_memory_map, &size,
996 mem_map, &key, &desc_size, &desc_version);
997 if (status == EFI_BUFFER_TOO_SMALL) {
998 low_free(_size, (unsigned long)mem_map);
999 goto again;
1000 }
1001
1002 if (status != EFI_SUCCESS)
1003 goto free_mem_map;
1004
Matt Fleming9ca8f722012-07-19 10:23:48 +01001005 memcpy(&efi->efi_loader_signature, EFI_LOADER_SIGNATURE, sizeof(__u32));
Matt Fleming291f3632011-12-12 21:27:52 +00001006 efi->efi_systab = (unsigned long)sys_table;
1007 efi->efi_memdesc_size = desc_size;
1008 efi->efi_memdesc_version = desc_version;
1009 efi->efi_memmap = (unsigned long)mem_map;
1010 efi->efi_memmap_size = size;
1011
1012#ifdef CONFIG_X86_64
1013 efi->efi_systab_hi = (unsigned long)sys_table >> 32;
1014 efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
1015#endif
1016
1017 /* Might as well exit boot services now */
1018 status = efi_call_phys2(sys_table->boottime->exit_boot_services,
1019 handle, key);
1020 if (status != EFI_SUCCESS)
1021 goto free_mem_map;
1022
1023 /* Historic? */
1024 boot_params->alt_mem_k = 32 * 1024;
1025
1026 /*
1027 * Convert the EFI memory map to E820.
1028 */
1029 nr_entries = 0;
1030 for (i = 0; i < size / desc_size; i++) {
1031 efi_memory_desc_t *d;
1032 unsigned int e820_type = 0;
1033 unsigned long m = (unsigned long)mem_map;
1034
1035 d = (efi_memory_desc_t *)(m + (i * desc_size));
1036 switch (d->type) {
1037 case EFI_RESERVED_TYPE:
1038 case EFI_RUNTIME_SERVICES_CODE:
1039 case EFI_RUNTIME_SERVICES_DATA:
1040 case EFI_MEMORY_MAPPED_IO:
1041 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1042 case EFI_PAL_CODE:
1043 e820_type = E820_RESERVED;
1044 break;
1045
1046 case EFI_UNUSABLE_MEMORY:
1047 e820_type = E820_UNUSABLE;
1048 break;
1049
1050 case EFI_ACPI_RECLAIM_MEMORY:
1051 e820_type = E820_ACPI;
1052 break;
1053
1054 case EFI_LOADER_CODE:
1055 case EFI_LOADER_DATA:
1056 case EFI_BOOT_SERVICES_CODE:
1057 case EFI_BOOT_SERVICES_DATA:
1058 case EFI_CONVENTIONAL_MEMORY:
1059 e820_type = E820_RAM;
1060 break;
1061
1062 case EFI_ACPI_MEMORY_NVS:
1063 e820_type = E820_NVS;
1064 break;
1065
1066 default:
1067 continue;
1068 }
1069
1070 /* Merge adjacent mappings */
1071 if (prev && prev->type == e820_type &&
1072 (prev->addr + prev->size) == d->phys_addr)
1073 prev->size += d->num_pages << 12;
1074 else {
1075 e820_map->addr = d->phys_addr;
1076 e820_map->size = d->num_pages << 12;
1077 e820_map->type = e820_type;
1078 prev = e820_map++;
1079 nr_entries++;
1080 }
1081 }
1082
1083 boot_params->e820_entries = nr_entries;
1084
1085 return EFI_SUCCESS;
1086
1087free_mem_map:
1088 low_free(_size, (unsigned long)mem_map);
Matt Fleming291f3632011-12-12 21:27:52 +00001089 return status;
1090}
1091
Matt Fleming9ca8f722012-07-19 10:23:48 +01001092static efi_status_t relocate_kernel(struct setup_header *hdr)
Matt Fleming291f3632011-12-12 21:27:52 +00001093{
Matt Fleming291f3632011-12-12 21:27:52 +00001094 unsigned long start, nr_pages;
Matt Fleming291f3632011-12-12 21:27:52 +00001095 efi_status_t status;
Matt Fleminge31be362012-03-23 09:35:05 -07001096
Matt Fleming291f3632011-12-12 21:27:52 +00001097 /*
1098 * The EFI firmware loader could have placed the kernel image
1099 * anywhere in memory, but the kernel has various restrictions
1100 * on the max physical address it can run at. Attempt to move
1101 * the kernel to boot_params.pref_address, or as low as
1102 * possible.
1103 */
1104 start = hdr->pref_address;
1105 nr_pages = round_up(hdr->init_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
1106
1107 status = efi_call_phys4(sys_table->boottime->allocate_pages,
1108 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
1109 nr_pages, &start);
1110 if (status != EFI_SUCCESS) {
1111 status = low_alloc(hdr->init_size, hdr->kernel_alignment,
1112 &start);
Matt Fleming9ca8f722012-07-19 10:23:48 +01001113 if (status != EFI_SUCCESS)
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001114 efi_printk("Failed to alloc mem for kernel\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001115 }
1116
Matt Fleming9ca8f722012-07-19 10:23:48 +01001117 if (status == EFI_SUCCESS)
1118 memcpy((void *)start, (void *)(unsigned long)hdr->code32_start,
1119 hdr->init_size);
Matt Fleming291f3632011-12-12 21:27:52 +00001120
Matt Fleming9ca8f722012-07-19 10:23:48 +01001121 hdr->pref_address = hdr->code32_start;
1122 hdr->code32_start = (__u32)start;
1123
1124 return status;
1125}
1126
1127/*
1128 * On success we return a pointer to a boot_params structure, and NULL
1129 * on failure.
1130 */
1131struct boot_params *efi_main(void *handle, efi_system_table_t *_table,
1132 struct boot_params *boot_params)
1133{
1134 struct desc_ptr *gdt, *idt;
1135 efi_loaded_image_t *image;
1136 struct setup_header *hdr = &boot_params->hdr;
1137 efi_status_t status;
1138 struct desc_struct *desc;
1139
1140 sys_table = _table;
1141
1142 /* Check if we were booted by the EFI firmware */
1143 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1144 goto fail;
1145
1146 setup_graphics(boot_params);
Matt Fleming291f3632011-12-12 21:27:52 +00001147
Matthew Garrettdd5fc852012-12-05 14:33:26 -07001148 setup_efi_pci(boot_params);
1149
Matt Fleming291f3632011-12-12 21:27:52 +00001150 status = efi_call_phys3(sys_table->boottime->allocate_pool,
1151 EFI_LOADER_DATA, sizeof(*gdt),
1152 (void **)&gdt);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001153 if (status != EFI_SUCCESS) {
1154 efi_printk("Failed to alloc mem for gdt structure\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001155 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001156 }
Matt Fleming291f3632011-12-12 21:27:52 +00001157
1158 gdt->size = 0x800;
1159 status = low_alloc(gdt->size, 8, (unsigned long *)&gdt->address);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001160 if (status != EFI_SUCCESS) {
1161 efi_printk("Failed to alloc mem for gdt\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001162 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001163 }
Matt Fleming291f3632011-12-12 21:27:52 +00001164
1165 status = efi_call_phys3(sys_table->boottime->allocate_pool,
1166 EFI_LOADER_DATA, sizeof(*idt),
1167 (void **)&idt);
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001168 if (status != EFI_SUCCESS) {
1169 efi_printk("Failed to alloc mem for idt structure\n");
Matt Fleming291f3632011-12-12 21:27:52 +00001170 goto fail;
Matt Fleming9fa7ded2012-02-20 13:20:59 +00001171 }
Matt Fleming291f3632011-12-12 21:27:52 +00001172
1173 idt->size = 0;
1174 idt->address = 0;
1175
Matt Fleming9ca8f722012-07-19 10:23:48 +01001176 /*
1177 * If the kernel isn't already loaded at the preferred load
1178 * address, relocate it.
1179 */
1180 if (hdr->pref_address != hdr->code32_start) {
1181 status = relocate_kernel(hdr);
1182
1183 if (status != EFI_SUCCESS)
1184 goto fail;
1185 }
1186
1187 status = exit_boot(boot_params, handle);
Matt Fleming291f3632011-12-12 21:27:52 +00001188 if (status != EFI_SUCCESS)
1189 goto fail;
1190
1191 memset((char *)gdt->address, 0x0, gdt->size);
1192 desc = (struct desc_struct *)gdt->address;
1193
1194 /* The first GDT is a dummy and the second is unused. */
1195 desc += 2;
1196
1197 desc->limit0 = 0xffff;
1198 desc->base0 = 0x0000;
1199 desc->base1 = 0x0000;
1200 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1201 desc->s = DESC_TYPE_CODE_DATA;
1202 desc->dpl = 0;
1203 desc->p = 1;
1204 desc->limit = 0xf;
1205 desc->avl = 0;
1206 desc->l = 0;
1207 desc->d = SEG_OP_SIZE_32BIT;
1208 desc->g = SEG_GRANULARITY_4KB;
1209 desc->base2 = 0x00;
1210
1211 desc++;
1212 desc->limit0 = 0xffff;
1213 desc->base0 = 0x0000;
1214 desc->base1 = 0x0000;
1215 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1216 desc->s = DESC_TYPE_CODE_DATA;
1217 desc->dpl = 0;
1218 desc->p = 1;
1219 desc->limit = 0xf;
1220 desc->avl = 0;
1221 desc->l = 0;
1222 desc->d = SEG_OP_SIZE_32BIT;
1223 desc->g = SEG_GRANULARITY_4KB;
1224 desc->base2 = 0x00;
1225
1226#ifdef CONFIG_X86_64
1227 /* Task segment value */
1228 desc++;
1229 desc->limit0 = 0x0000;
1230 desc->base0 = 0x0000;
1231 desc->base1 = 0x0000;
1232 desc->type = SEG_TYPE_TSS;
1233 desc->s = 0;
1234 desc->dpl = 0;
1235 desc->p = 1;
1236 desc->limit = 0x0;
1237 desc->avl = 0;
1238 desc->l = 0;
1239 desc->d = 0;
1240 desc->g = SEG_GRANULARITY_4KB;
1241 desc->base2 = 0x00;
1242#endif /* CONFIG_X86_64 */
1243
1244 asm volatile ("lidt %0" : : "m" (*idt));
1245 asm volatile ("lgdt %0" : : "m" (*gdt));
1246
1247 asm volatile("cli");
1248
1249 return boot_params;
1250fail:
1251 return NULL;
1252}