multiboot_mbi2.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008,2009,2010,2011,2012,2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/memory.h>
  19. #ifdef GRUB_MACHINE_PCBIOS
  20. #include <grub/machine/biosnum.h>
  21. #include <grub/machine/apm.h>
  22. #include <grub/machine/memory.h>
  23. #endif
  24. #include <grub/multiboot.h>
  25. #include <grub/cpu/multiboot.h>
  26. #include <grub/cpu/relocator.h>
  27. #include <grub/disk.h>
  28. #include <grub/device.h>
  29. #include <grub/partition.h>
  30. #include <grub/mm.h>
  31. #include <grub/misc.h>
  32. #include <grub/env.h>
  33. #include <grub/video.h>
  34. #include <grub/acpi.h>
  35. #include <grub/i18n.h>
  36. #include <grub/net.h>
  37. #include <grub/lib/cmdline.h>
  38. #if defined (GRUB_MACHINE_EFI)
  39. #include <grub/efi/efi.h>
  40. #endif
  41. #if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_MULTIBOOT) || defined (GRUB_MACHINE_QEMU)
  42. #include <grub/i386/pc/vbe.h>
  43. #define HAS_VGA_TEXT 1
  44. #else
  45. #define HAS_VGA_TEXT 0
  46. #endif
  47. struct module
  48. {
  49. struct module *next;
  50. grub_addr_t start;
  51. grub_size_t size;
  52. char *cmdline;
  53. int cmdline_size;
  54. };
  55. static struct module *modules, *modules_last;
  56. static grub_size_t cmdline_size;
  57. static grub_size_t total_modcmd;
  58. static unsigned modcnt;
  59. static char *cmdline = NULL;
  60. static int bootdev_set;
  61. static grub_uint32_t biosdev, slice, part;
  62. static grub_size_t elf_sec_num, elf_sec_entsize;
  63. static unsigned elf_sec_shstrndx;
  64. static void *elf_sections;
  65. static int keep_bs = 0;
  66. static grub_uint32_t load_base_addr;
  67. void
  68. grub_multiboot_add_elfsyms (grub_size_t num, grub_size_t entsize,
  69. unsigned shndx, void *data)
  70. {
  71. elf_sec_num = num;
  72. elf_sec_shstrndx = shndx;
  73. elf_sec_entsize = entsize;
  74. elf_sections = data;
  75. }
  76. static struct multiboot_header *
  77. find_header (grub_properly_aligned_t *buffer, grub_ssize_t len)
  78. {
  79. struct multiboot_header *header;
  80. /* Look for the multiboot header in the buffer. The header should
  81. be at least 12 bytes and aligned on a 4-byte boundary. */
  82. for (header = (struct multiboot_header *) buffer;
  83. ((char *) header <= (char *) buffer + len - 12);
  84. header = (struct multiboot_header *) ((grub_uint32_t *) header + MULTIBOOT_HEADER_ALIGN / 4))
  85. {
  86. if (header->magic == MULTIBOOT_HEADER_MAGIC
  87. && !(header->magic + header->architecture
  88. + header->header_length + header->checksum)
  89. && header->architecture == MULTIBOOT_ARCHITECTURE_CURRENT)
  90. return header;
  91. }
  92. return NULL;
  93. }
  94. grub_err_t
  95. grub_multiboot_load (grub_file_t file, const char *filename)
  96. {
  97. grub_ssize_t len;
  98. struct multiboot_header *header;
  99. grub_err_t err;
  100. struct multiboot_header_tag *tag;
  101. struct multiboot_header_tag_address *addr_tag = NULL;
  102. struct multiboot_header_tag_relocatable *rel_tag;
  103. int entry_specified = 0, efi_entry_specified = 0;
  104. grub_addr_t entry = 0, efi_entry = 0;
  105. grub_uint32_t console_required = 0;
  106. struct multiboot_header_tag_framebuffer *fbtag = NULL;
  107. int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;
  108. mbi_load_data_t mld;
  109. mld.mbi_ver = 2;
  110. mld.relocatable = 0;
  111. mld.buffer = grub_malloc (MULTIBOOT_SEARCH);
  112. if (!mld.buffer)
  113. return grub_errno;
  114. len = grub_file_read (file, mld.buffer, MULTIBOOT_SEARCH);
  115. if (len < 32)
  116. {
  117. grub_free (mld.buffer);
  118. return grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), filename);
  119. }
  120. COMPILE_TIME_ASSERT (MULTIBOOT_HEADER_ALIGN % 4 == 0);
  121. header = find_header (mld.buffer, len);
  122. if (header == 0)
  123. {
  124. grub_free (mld.buffer);
  125. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no multiboot header found");
  126. }
  127. COMPILE_TIME_ASSERT (MULTIBOOT_TAG_ALIGN % 4 == 0);
  128. keep_bs = 0;
  129. for (tag = (struct multiboot_header_tag *) (header + 1);
  130. tag->type != MULTIBOOT_TAG_TYPE_END;
  131. tag = (struct multiboot_header_tag *) ((grub_uint32_t *) tag + ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN) / 4))
  132. switch (tag->type)
  133. {
  134. case MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST:
  135. {
  136. unsigned i;
  137. struct multiboot_header_tag_information_request *request_tag
  138. = (struct multiboot_header_tag_information_request *) tag;
  139. if (request_tag->flags & MULTIBOOT_HEADER_TAG_OPTIONAL)
  140. break;
  141. for (i = 0; i < (request_tag->size - sizeof (*request_tag))
  142. / sizeof (request_tag->requests[0]); i++)
  143. switch (request_tag->requests[i])
  144. {
  145. case MULTIBOOT_TAG_TYPE_END:
  146. case MULTIBOOT_TAG_TYPE_CMDLINE:
  147. case MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME:
  148. case MULTIBOOT_TAG_TYPE_MODULE:
  149. case MULTIBOOT_TAG_TYPE_BASIC_MEMINFO:
  150. case MULTIBOOT_TAG_TYPE_BOOTDEV:
  151. case MULTIBOOT_TAG_TYPE_MMAP:
  152. case MULTIBOOT_TAG_TYPE_FRAMEBUFFER:
  153. case MULTIBOOT_TAG_TYPE_VBE:
  154. case MULTIBOOT_TAG_TYPE_ELF_SECTIONS:
  155. case MULTIBOOT_TAG_TYPE_APM:
  156. case MULTIBOOT_TAG_TYPE_EFI32:
  157. case MULTIBOOT_TAG_TYPE_EFI64:
  158. case MULTIBOOT_TAG_TYPE_ACPI_OLD:
  159. case MULTIBOOT_TAG_TYPE_ACPI_NEW:
  160. case MULTIBOOT_TAG_TYPE_NETWORK:
  161. case MULTIBOOT_TAG_TYPE_EFI_MMAP:
  162. case MULTIBOOT_TAG_TYPE_EFI_BS:
  163. case MULTIBOOT_TAG_TYPE_EFI32_IH:
  164. case MULTIBOOT_TAG_TYPE_EFI64_IH:
  165. case MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR:
  166. break;
  167. default:
  168. grub_free (mld.buffer);
  169. return grub_error (GRUB_ERR_UNKNOWN_OS,
  170. "unsupported information tag: 0x%x",
  171. request_tag->requests[i]);
  172. }
  173. break;
  174. }
  175. case MULTIBOOT_HEADER_TAG_ADDRESS:
  176. addr_tag = (struct multiboot_header_tag_address *) tag;
  177. break;
  178. case MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS:
  179. entry_specified = 1;
  180. entry = ((struct multiboot_header_tag_entry_address *) tag)->entry_addr;
  181. break;
  182. case MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64:
  183. #if defined (GRUB_MACHINE_EFI) && defined (__x86_64__)
  184. efi_entry_specified = 1;
  185. efi_entry = ((struct multiboot_header_tag_entry_address *) tag)->entry_addr;
  186. #endif
  187. break;
  188. case MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS:
  189. if (!(((struct multiboot_header_tag_console_flags *) tag)->console_flags
  190. & MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED))
  191. accepted_consoles &= ~GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;
  192. if (((struct multiboot_header_tag_console_flags *) tag)->console_flags
  193. & MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED)
  194. console_required = 1;
  195. break;
  196. case MULTIBOOT_HEADER_TAG_FRAMEBUFFER:
  197. fbtag = (struct multiboot_header_tag_framebuffer *) tag;
  198. accepted_consoles |= GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER;
  199. break;
  200. case MULTIBOOT_HEADER_TAG_RELOCATABLE:
  201. mld.relocatable = 1;
  202. rel_tag = (struct multiboot_header_tag_relocatable *) tag;
  203. mld.min_addr = rel_tag->min_addr;
  204. mld.max_addr = rel_tag->max_addr;
  205. mld.align = rel_tag->align;
  206. switch (rel_tag->preference)
  207. {
  208. case MULTIBOOT_LOAD_PREFERENCE_LOW:
  209. mld.preference = GRUB_RELOCATOR_PREFERENCE_LOW;
  210. break;
  211. case MULTIBOOT_LOAD_PREFERENCE_HIGH:
  212. mld.preference = GRUB_RELOCATOR_PREFERENCE_HIGH;
  213. break;
  214. default:
  215. mld.preference = GRUB_RELOCATOR_PREFERENCE_NONE;
  216. }
  217. break;
  218. /* GRUB always page-aligns modules. */
  219. case MULTIBOOT_HEADER_TAG_MODULE_ALIGN:
  220. break;
  221. case MULTIBOOT_HEADER_TAG_EFI_BS:
  222. #ifdef GRUB_MACHINE_EFI
  223. keep_bs = 1;
  224. #endif
  225. break;
  226. default:
  227. if (! (tag->flags & MULTIBOOT_HEADER_TAG_OPTIONAL))
  228. {
  229. grub_free (mld.buffer);
  230. return grub_error (GRUB_ERR_UNKNOWN_OS,
  231. "unsupported tag: 0x%x", tag->type);
  232. }
  233. break;
  234. }
  235. if (addr_tag && !entry_specified && !(keep_bs && efi_entry_specified))
  236. {
  237. grub_free (mld.buffer);
  238. return grub_error (GRUB_ERR_UNKNOWN_OS,
  239. "load address tag without entry address tag");
  240. }
  241. if (addr_tag)
  242. {
  243. grub_uint64_t load_addr = (addr_tag->load_addr + 1)
  244. ? addr_tag->load_addr : (addr_tag->header_addr
  245. - ((char *) header - (char *) mld.buffer));
  246. int offset = ((char *) header - (char *) mld.buffer -
  247. (addr_tag->header_addr - load_addr));
  248. int load_size = ((addr_tag->load_end_addr == 0) ? file->size - offset :
  249. addr_tag->load_end_addr - addr_tag->load_addr);
  250. grub_size_t code_size;
  251. void *source;
  252. grub_relocator_chunk_t ch;
  253. if (addr_tag->bss_end_addr)
  254. code_size = (addr_tag->bss_end_addr - load_addr);
  255. else
  256. code_size = load_size;
  257. if (mld.relocatable)
  258. {
  259. if (code_size > mld.max_addr || mld.min_addr > mld.max_addr - code_size)
  260. {
  261. grub_free (mld.buffer);
  262. return grub_error (GRUB_ERR_BAD_OS, "invalid min/max address and/or load size");
  263. }
  264. err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch,
  265. mld.min_addr, mld.max_addr - code_size,
  266. code_size, mld.align ? mld.align : 1,
  267. mld.preference, keep_bs);
  268. }
  269. else
  270. err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
  271. &ch, load_addr, code_size);
  272. if (err)
  273. {
  274. grub_dprintf ("multiboot_loader", "Error loading aout kludge\n");
  275. grub_free (mld.buffer);
  276. return err;
  277. }
  278. mld.link_base_addr = load_addr;
  279. mld.load_base_addr = get_physical_target_address (ch);
  280. source = get_virtual_current_address (ch);
  281. grub_dprintf ("multiboot_loader", "link_base_addr=0x%x, load_base_addr=0x%x, "
  282. "load_size=0x%lx, relocatable=%d\n", mld.link_base_addr,
  283. mld.load_base_addr, (long) code_size, mld.relocatable);
  284. if (mld.relocatable)
  285. grub_dprintf ("multiboot_loader", "align=0x%lx, preference=0x%x, avoid_efi_boot_services=%d\n",
  286. (long) mld.align, mld.preference, keep_bs);
  287. if ((grub_file_seek (file, offset)) == (grub_off_t) -1)
  288. {
  289. grub_free (mld.buffer);
  290. return grub_errno;
  291. }
  292. grub_file_read (file, source, load_size);
  293. if (grub_errno)
  294. {
  295. grub_free (mld.buffer);
  296. return grub_errno;
  297. }
  298. if (addr_tag->bss_end_addr)
  299. grub_memset ((grub_uint8_t *) source + load_size, 0,
  300. addr_tag->bss_end_addr - load_addr - load_size);
  301. }
  302. else
  303. {
  304. mld.file = file;
  305. mld.filename = filename;
  306. mld.avoid_efi_boot_services = keep_bs;
  307. err = grub_multiboot_load_elf (&mld);
  308. if (err)
  309. {
  310. grub_free (mld.buffer);
  311. return err;
  312. }
  313. }
  314. load_base_addr = mld.load_base_addr;
  315. if (keep_bs && efi_entry_specified)
  316. grub_multiboot_payload_eip = efi_entry;
  317. else if (entry_specified)
  318. grub_multiboot_payload_eip = entry;
  319. if (mld.relocatable)
  320. {
  321. /*
  322. * Both branches are mathematically equivalent. However, it looks
  323. * that real life (C?) is more complicated. I am trying to avoid
  324. * wrap around here if mld.load_base_addr < mld.link_base_addr.
  325. * If you look at C operator precedence then everything should work.
  326. * However, I am not 100% sure that a given compiler will not
  327. * optimize/break this stuff. So, maybe we should use signed
  328. * 64-bit int here.
  329. */
  330. if (mld.load_base_addr >= mld.link_base_addr)
  331. grub_multiboot_payload_eip += mld.load_base_addr - mld.link_base_addr;
  332. else
  333. grub_multiboot_payload_eip -= mld.link_base_addr - mld.load_base_addr;
  334. }
  335. if (fbtag)
  336. err = grub_multiboot_set_console (GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER,
  337. accepted_consoles,
  338. fbtag->width, fbtag->height,
  339. fbtag->depth, console_required);
  340. else
  341. err = grub_multiboot_set_console (GRUB_MULTIBOOT_CONSOLE_EGA_TEXT,
  342. accepted_consoles,
  343. 0, 0, 0, console_required);
  344. return err;
  345. }
  346. static grub_size_t
  347. acpiv2_size (void)
  348. {
  349. #if GRUB_MACHINE_HAS_ACPI
  350. struct grub_acpi_rsdp_v20 *p = grub_acpi_get_rsdpv2 ();
  351. if (!p)
  352. return 0;
  353. return ALIGN_UP (sizeof (struct multiboot_tag_old_acpi)
  354. + p->length, MULTIBOOT_TAG_ALIGN);
  355. #else
  356. return 0;
  357. #endif
  358. }
  359. #ifdef GRUB_MACHINE_EFI
  360. static grub_efi_uintn_t efi_mmap_size = 0;
  361. /* Find the optimal number of pages for the memory map. Is it better to
  362. move this code to efi/mm.c? */
  363. static void
  364. find_efi_mmap_size (void)
  365. {
  366. efi_mmap_size = (1 << 12);
  367. while (1)
  368. {
  369. int ret;
  370. grub_efi_memory_descriptor_t *mmap;
  371. grub_efi_uintn_t desc_size;
  372. grub_efi_uintn_t cur_mmap_size = efi_mmap_size;
  373. mmap = grub_malloc (cur_mmap_size);
  374. if (! mmap)
  375. return;
  376. ret = grub_efi_get_memory_map (&cur_mmap_size, mmap, 0, &desc_size, 0);
  377. grub_free (mmap);
  378. if (ret < 0)
  379. return;
  380. else if (ret > 0)
  381. break;
  382. if (efi_mmap_size < cur_mmap_size)
  383. efi_mmap_size = cur_mmap_size;
  384. efi_mmap_size += (1 << 12);
  385. }
  386. /* Increase the size a bit for safety, because GRUB allocates more on
  387. later, and EFI itself may allocate more. */
  388. efi_mmap_size += (3 << 12);
  389. efi_mmap_size = ALIGN_UP (efi_mmap_size, 4096);
  390. }
  391. #endif
  392. static grub_size_t
  393. net_size (void)
  394. {
  395. struct grub_net_network_level_interface *net;
  396. grub_size_t ret = 0;
  397. FOR_NET_NETWORK_LEVEL_INTERFACES(net)
  398. if (net->dhcp_ack)
  399. ret += ALIGN_UP (sizeof (struct multiboot_tag_network) + net->dhcp_acklen,
  400. MULTIBOOT_TAG_ALIGN);
  401. return ret;
  402. }
  403. static grub_size_t
  404. grub_multiboot_get_mbi_size (void)
  405. {
  406. #ifdef GRUB_MACHINE_EFI
  407. if (!keep_bs && !efi_mmap_size)
  408. find_efi_mmap_size ();
  409. #endif
  410. return 2 * sizeof (grub_uint32_t) + sizeof (struct multiboot_tag)
  411. + sizeof (struct multiboot_tag)
  412. + (sizeof (struct multiboot_tag_string)
  413. + ALIGN_UP (cmdline_size, MULTIBOOT_TAG_ALIGN))
  414. + (sizeof (struct multiboot_tag_string)
  415. + ALIGN_UP (sizeof (PACKAGE_STRING), MULTIBOOT_TAG_ALIGN))
  416. + (modcnt * sizeof (struct multiboot_tag_module) + total_modcmd)
  417. + ALIGN_UP (sizeof (struct multiboot_tag_basic_meminfo),
  418. MULTIBOOT_TAG_ALIGN)
  419. + ALIGN_UP (sizeof (struct multiboot_tag_bootdev), MULTIBOOT_TAG_ALIGN)
  420. + ALIGN_UP (sizeof (struct multiboot_tag_elf_sections), MULTIBOOT_TAG_ALIGN)
  421. + ALIGN_UP (elf_sec_entsize * elf_sec_num, MULTIBOOT_TAG_ALIGN)
  422. + ALIGN_UP ((sizeof (struct multiboot_tag_mmap)
  423. + grub_get_multiboot_mmap_count ()
  424. * sizeof (struct multiboot_mmap_entry)), MULTIBOOT_TAG_ALIGN)
  425. + ALIGN_UP (sizeof (struct multiboot_tag_framebuffer), MULTIBOOT_TAG_ALIGN)
  426. + ALIGN_UP (sizeof (struct multiboot_tag_old_acpi)
  427. + sizeof (struct grub_acpi_rsdp_v10), MULTIBOOT_TAG_ALIGN)
  428. + ALIGN_UP (sizeof (struct multiboot_tag_load_base_addr), MULTIBOOT_TAG_ALIGN)
  429. + acpiv2_size ()
  430. + net_size ()
  431. #ifdef GRUB_MACHINE_EFI
  432. + ALIGN_UP (sizeof (struct multiboot_tag_efi32), MULTIBOOT_TAG_ALIGN)
  433. + ALIGN_UP (sizeof (struct multiboot_tag_efi32_ih), MULTIBOOT_TAG_ALIGN)
  434. + ALIGN_UP (sizeof (struct multiboot_tag_efi64), MULTIBOOT_TAG_ALIGN)
  435. + ALIGN_UP (sizeof (struct multiboot_tag_efi64_ih), MULTIBOOT_TAG_ALIGN)
  436. + ALIGN_UP (sizeof (struct multiboot_tag_efi_mmap)
  437. + efi_mmap_size, MULTIBOOT_TAG_ALIGN)
  438. #endif
  439. + sizeof (struct multiboot_tag_vbe) + MULTIBOOT_TAG_ALIGN - 1
  440. + sizeof (struct multiboot_tag_apm) + MULTIBOOT_TAG_ALIGN - 1;
  441. }
  442. /* Helper for grub_fill_multiboot_mmap. */
  443. static int
  444. grub_fill_multiboot_mmap_iter (grub_uint64_t addr, grub_uint64_t size,
  445. grub_memory_type_t type, void *data)
  446. {
  447. struct multiboot_mmap_entry **mmap_entry = data;
  448. (*mmap_entry)->addr = addr;
  449. (*mmap_entry)->len = size;
  450. (*mmap_entry)->type = type;
  451. (*mmap_entry)->zero = 0;
  452. (*mmap_entry)++;
  453. return 0;
  454. }
  455. /* Fill previously allocated Multiboot mmap. */
  456. static void
  457. grub_fill_multiboot_mmap (struct multiboot_tag_mmap *tag)
  458. {
  459. struct multiboot_mmap_entry *mmap_entry = tag->entries;
  460. tag->type = MULTIBOOT_TAG_TYPE_MMAP;
  461. tag->size = sizeof (struct multiboot_tag_mmap)
  462. + sizeof (struct multiboot_mmap_entry) * grub_get_multiboot_mmap_count ();
  463. tag->entry_size = sizeof (struct multiboot_mmap_entry);
  464. tag->entry_version = 0;
  465. grub_mmap_iterate (grub_fill_multiboot_mmap_iter, &mmap_entry);
  466. }
  467. #if defined (GRUB_MACHINE_PCBIOS)
  468. static void
  469. fill_vbe_tag (struct multiboot_tag_vbe *tag)
  470. {
  471. grub_vbe_status_t status;
  472. void *scratch = (void *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR;
  473. tag->type = MULTIBOOT_TAG_TYPE_VBE;
  474. tag->size = 0;
  475. status = grub_vbe_bios_get_controller_info (scratch);
  476. if (status != GRUB_VBE_STATUS_OK)
  477. return;
  478. grub_memcpy (&tag->vbe_control_info, scratch,
  479. sizeof (struct grub_vbe_info_block));
  480. status = grub_vbe_bios_get_mode (scratch);
  481. tag->vbe_mode = *(grub_uint32_t *) scratch;
  482. if (status != GRUB_VBE_STATUS_OK)
  483. return;
  484. /* get_mode_info isn't available for mode 3. */
  485. if (tag->vbe_mode == 3)
  486. {
  487. struct grub_vbe_mode_info_block *mode_info = (void *) &tag->vbe_mode_info;
  488. grub_memset (mode_info, 0,
  489. sizeof (struct grub_vbe_mode_info_block));
  490. mode_info->memory_model = GRUB_VBE_MEMORY_MODEL_TEXT;
  491. mode_info->x_resolution = 80;
  492. mode_info->y_resolution = 25;
  493. }
  494. else
  495. {
  496. status = grub_vbe_bios_get_mode_info (tag->vbe_mode, scratch);
  497. if (status != GRUB_VBE_STATUS_OK)
  498. return;
  499. grub_memcpy (&tag->vbe_mode_info, scratch,
  500. sizeof (struct grub_vbe_mode_info_block));
  501. }
  502. grub_vbe_bios_get_pm_interface (&tag->vbe_interface_seg,
  503. &tag->vbe_interface_off,
  504. &tag->vbe_interface_len);
  505. tag->size = sizeof (*tag);
  506. }
  507. #endif
  508. static grub_err_t
  509. retrieve_video_parameters (grub_properly_aligned_t **ptrorig)
  510. {
  511. grub_err_t err;
  512. struct grub_video_mode_info mode_info;
  513. void *framebuffer;
  514. grub_video_driver_id_t driv_id;
  515. struct grub_video_palette_data palette[256];
  516. struct multiboot_tag_framebuffer *tag
  517. = (struct multiboot_tag_framebuffer *) *ptrorig;
  518. err = grub_multiboot_set_video_mode ();
  519. if (err)
  520. {
  521. grub_print_error ();
  522. grub_errno = GRUB_ERR_NONE;
  523. }
  524. grub_video_get_palette (0, ARRAY_SIZE (palette), palette);
  525. driv_id = grub_video_get_driver_id ();
  526. #if HAS_VGA_TEXT
  527. if (driv_id == GRUB_VIDEO_DRIVER_NONE)
  528. {
  529. struct grub_vbe_mode_info_block vbe_mode_info;
  530. grub_uint32_t vbe_mode;
  531. #if defined (GRUB_MACHINE_PCBIOS)
  532. {
  533. grub_vbe_status_t status;
  534. void *scratch = (void *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR;
  535. status = grub_vbe_bios_get_mode (scratch);
  536. vbe_mode = *(grub_uint32_t *) scratch;
  537. if (status != GRUB_VBE_STATUS_OK)
  538. return GRUB_ERR_NONE;
  539. }
  540. #else
  541. vbe_mode = 3;
  542. #endif
  543. /* get_mode_info isn't available for mode 3. */
  544. if (vbe_mode == 3)
  545. {
  546. grub_memset (&vbe_mode_info, 0,
  547. sizeof (struct grub_vbe_mode_info_block));
  548. vbe_mode_info.memory_model = GRUB_VBE_MEMORY_MODEL_TEXT;
  549. vbe_mode_info.x_resolution = 80;
  550. vbe_mode_info.y_resolution = 25;
  551. }
  552. #if defined (GRUB_MACHINE_PCBIOS)
  553. else
  554. {
  555. grub_vbe_status_t status;
  556. void *scratch = (void *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR;
  557. status = grub_vbe_bios_get_mode_info (vbe_mode, scratch);
  558. if (status != GRUB_VBE_STATUS_OK)
  559. return GRUB_ERR_NONE;
  560. grub_memcpy (&vbe_mode_info, scratch,
  561. sizeof (struct grub_vbe_mode_info_block));
  562. }
  563. #endif
  564. if (vbe_mode_info.memory_model == GRUB_VBE_MEMORY_MODEL_TEXT)
  565. {
  566. tag = (struct multiboot_tag_framebuffer *) *ptrorig;
  567. tag->common.type = MULTIBOOT_TAG_TYPE_FRAMEBUFFER;
  568. tag->common.size = 0;
  569. tag->common.framebuffer_addr = 0xb8000;
  570. tag->common.framebuffer_pitch = 2 * vbe_mode_info.x_resolution;
  571. tag->common.framebuffer_width = vbe_mode_info.x_resolution;
  572. tag->common.framebuffer_height = vbe_mode_info.y_resolution;
  573. tag->common.framebuffer_bpp = 16;
  574. tag->common.framebuffer_type = MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT;
  575. tag->common.size = sizeof (tag->common);
  576. tag->common.reserved = 0;
  577. *ptrorig += ALIGN_UP (tag->common.size, MULTIBOOT_TAG_ALIGN)
  578. / sizeof (grub_properly_aligned_t);
  579. }
  580. return GRUB_ERR_NONE;
  581. }
  582. #else
  583. if (driv_id == GRUB_VIDEO_DRIVER_NONE)
  584. return GRUB_ERR_NONE;
  585. #endif
  586. #if GRUB_MACHINE_HAS_VBE
  587. {
  588. struct multiboot_tag_vbe *tag_vbe = (struct multiboot_tag_vbe *) *ptrorig;
  589. fill_vbe_tag (tag_vbe);
  590. *ptrorig += ALIGN_UP (tag_vbe->size, MULTIBOOT_TAG_ALIGN)
  591. / sizeof (grub_properly_aligned_t);
  592. }
  593. #endif
  594. err = grub_video_get_info_and_fini (&mode_info, &framebuffer);
  595. if (err)
  596. return err;
  597. tag = (struct multiboot_tag_framebuffer *) *ptrorig;
  598. tag->common.type = MULTIBOOT_TAG_TYPE_FRAMEBUFFER;
  599. tag->common.size = 0;
  600. tag->common.framebuffer_addr = (grub_addr_t) framebuffer;
  601. tag->common.framebuffer_pitch = mode_info.pitch;
  602. tag->common.framebuffer_width = mode_info.width;
  603. tag->common.framebuffer_height = mode_info.height;
  604. tag->common.framebuffer_bpp = mode_info.bpp;
  605. tag->common.reserved = 0;
  606. if (mode_info.mode_type & GRUB_VIDEO_MODE_TYPE_INDEX_COLOR)
  607. {
  608. unsigned i;
  609. tag->common.framebuffer_type = MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED;
  610. tag->framebuffer_palette_num_colors = mode_info.number_of_colors;
  611. if (tag->framebuffer_palette_num_colors > ARRAY_SIZE (palette))
  612. tag->framebuffer_palette_num_colors = ARRAY_SIZE (palette);
  613. tag->common.size = sizeof (struct multiboot_tag_framebuffer_common)
  614. + sizeof (multiboot_uint16_t) + tag->framebuffer_palette_num_colors
  615. * sizeof (struct multiboot_color);
  616. for (i = 0; i < tag->framebuffer_palette_num_colors; i++)
  617. {
  618. tag->framebuffer_palette[i].red = palette[i].r;
  619. tag->framebuffer_palette[i].green = palette[i].g;
  620. tag->framebuffer_palette[i].blue = palette[i].b;
  621. }
  622. }
  623. else
  624. {
  625. tag->common.framebuffer_type = MULTIBOOT_FRAMEBUFFER_TYPE_RGB;
  626. tag->framebuffer_red_field_position = mode_info.red_field_pos;
  627. tag->framebuffer_red_mask_size = mode_info.red_mask_size;
  628. tag->framebuffer_green_field_position = mode_info.green_field_pos;
  629. tag->framebuffer_green_mask_size = mode_info.green_mask_size;
  630. tag->framebuffer_blue_field_position = mode_info.blue_field_pos;
  631. tag->framebuffer_blue_mask_size = mode_info.blue_mask_size;
  632. tag->common.size = sizeof (struct multiboot_tag_framebuffer_common) + 6;
  633. }
  634. *ptrorig += ALIGN_UP (tag->common.size, MULTIBOOT_TAG_ALIGN)
  635. / sizeof (grub_properly_aligned_t);
  636. return GRUB_ERR_NONE;
  637. }
  638. grub_err_t
  639. grub_multiboot_make_mbi (grub_uint32_t *target)
  640. {
  641. grub_properly_aligned_t *ptrorig;
  642. grub_properly_aligned_t *mbistart;
  643. grub_err_t err;
  644. grub_size_t bufsize;
  645. grub_relocator_chunk_t ch;
  646. bufsize = grub_multiboot_get_mbi_size ();
  647. COMPILE_TIME_ASSERT (MULTIBOOT_TAG_ALIGN % sizeof (grub_properly_aligned_t) == 0);
  648. err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch,
  649. 0, 0xffffffff - bufsize,
  650. bufsize, MULTIBOOT_TAG_ALIGN,
  651. GRUB_RELOCATOR_PREFERENCE_NONE, 1);
  652. if (err)
  653. return err;
  654. ptrorig = get_virtual_current_address (ch);
  655. #if defined (__i386__) || defined (__x86_64__)
  656. *target = get_physical_target_address (ch);
  657. #elif defined (__mips)
  658. *target = get_physical_target_address (ch) | 0x80000000;
  659. #else
  660. #error Please complete this
  661. #endif
  662. mbistart = ptrorig;
  663. COMPILE_TIME_ASSERT ((2 * sizeof (grub_uint32_t))
  664. % sizeof (grub_properly_aligned_t) == 0);
  665. COMPILE_TIME_ASSERT (MULTIBOOT_TAG_ALIGN
  666. % sizeof (grub_properly_aligned_t) == 0);
  667. ptrorig += (2 * sizeof (grub_uint32_t)) / sizeof (grub_properly_aligned_t);
  668. {
  669. struct multiboot_tag_load_base_addr *tag = (struct multiboot_tag_load_base_addr *) ptrorig;
  670. tag->type = MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR;
  671. tag->size = sizeof (struct multiboot_tag_load_base_addr);
  672. tag->load_base_addr = load_base_addr;
  673. ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
  674. / sizeof (grub_properly_aligned_t);
  675. }
  676. {
  677. struct multiboot_tag_string *tag = (struct multiboot_tag_string *) ptrorig;
  678. tag->type = MULTIBOOT_TAG_TYPE_CMDLINE;
  679. tag->size = sizeof (struct multiboot_tag_string) + cmdline_size;
  680. grub_memcpy (tag->string, cmdline, cmdline_size);
  681. ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
  682. / sizeof (grub_properly_aligned_t);
  683. }
  684. {
  685. struct multiboot_tag_string *tag = (struct multiboot_tag_string *) ptrorig;
  686. tag->type = MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME;
  687. tag->size = sizeof (struct multiboot_tag_string) + sizeof (PACKAGE_STRING);
  688. grub_memcpy (tag->string, PACKAGE_STRING, sizeof (PACKAGE_STRING));
  689. ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
  690. / sizeof (grub_properly_aligned_t);
  691. }
  692. #ifdef GRUB_MACHINE_PCBIOS
  693. {
  694. struct grub_apm_info info;
  695. if (grub_apm_get_info (&info))
  696. {
  697. struct multiboot_tag_apm *tag = (struct multiboot_tag_apm *) ptrorig;
  698. tag->type = MULTIBOOT_TAG_TYPE_APM;
  699. tag->size = sizeof (struct multiboot_tag_apm);
  700. tag->cseg = info.cseg;
  701. tag->offset = info.offset;
  702. tag->cseg_16 = info.cseg_16;
  703. tag->dseg = info.dseg;
  704. tag->flags = info.flags;
  705. tag->cseg_len = info.cseg_len;
  706. tag->dseg_len = info.dseg_len;
  707. tag->cseg_16_len = info.cseg_16_len;
  708. tag->version = info.version;
  709. ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
  710. / sizeof (grub_properly_aligned_t);
  711. }
  712. }
  713. #endif
  714. {
  715. unsigned i;
  716. struct module *cur;
  717. for (i = 0, cur = modules; i < modcnt; i++, cur = cur->next)
  718. {
  719. struct multiboot_tag_module *tag
  720. = (struct multiboot_tag_module *) ptrorig;
  721. tag->type = MULTIBOOT_TAG_TYPE_MODULE;
  722. tag->size = sizeof (struct multiboot_tag_module) + cur->cmdline_size;
  723. tag->mod_start = cur->start;
  724. tag->mod_end = tag->mod_start + cur->size;
  725. grub_memcpy (tag->cmdline, cur->cmdline, cur->cmdline_size);
  726. ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
  727. / sizeof (grub_properly_aligned_t);
  728. }
  729. }
  730. if (!keep_bs)
  731. {
  732. struct multiboot_tag_mmap *tag = (struct multiboot_tag_mmap *) ptrorig;
  733. grub_fill_multiboot_mmap (tag);
  734. ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
  735. / sizeof (grub_properly_aligned_t);
  736. }
  737. {
  738. struct multiboot_tag_elf_sections *tag
  739. = (struct multiboot_tag_elf_sections *) ptrorig;
  740. tag->type = MULTIBOOT_TAG_TYPE_ELF_SECTIONS;
  741. tag->size = sizeof (struct multiboot_tag_elf_sections)
  742. + elf_sec_entsize * elf_sec_num;
  743. grub_memcpy (tag->sections, elf_sections, elf_sec_entsize * elf_sec_num);
  744. tag->num = elf_sec_num;
  745. tag->entsize = elf_sec_entsize;
  746. tag->shndx = elf_sec_shstrndx;
  747. ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
  748. / sizeof (grub_properly_aligned_t);
  749. }
  750. if (!keep_bs)
  751. {
  752. struct multiboot_tag_basic_meminfo *tag
  753. = (struct multiboot_tag_basic_meminfo *) ptrorig;
  754. tag->type = MULTIBOOT_TAG_TYPE_BASIC_MEMINFO;
  755. tag->size = sizeof (struct multiboot_tag_basic_meminfo);
  756. /* Convert from bytes to kilobytes. */
  757. tag->mem_lower = grub_mmap_get_lower () / 1024;
  758. tag->mem_upper = grub_mmap_get_upper () / 1024;
  759. ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
  760. / sizeof (grub_properly_aligned_t);
  761. }
  762. {
  763. struct grub_net_network_level_interface *net;
  764. FOR_NET_NETWORK_LEVEL_INTERFACES(net)
  765. if (net->dhcp_ack)
  766. {
  767. struct multiboot_tag_network *tag
  768. = (struct multiboot_tag_network *) ptrorig;
  769. tag->type = MULTIBOOT_TAG_TYPE_NETWORK;
  770. tag->size = sizeof (struct multiboot_tag_network) + net->dhcp_acklen;
  771. grub_memcpy (tag->dhcpack, net->dhcp_ack, net->dhcp_acklen);
  772. ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
  773. / sizeof (grub_properly_aligned_t);
  774. }
  775. }
  776. if (bootdev_set)
  777. {
  778. struct multiboot_tag_bootdev *tag
  779. = (struct multiboot_tag_bootdev *) ptrorig;
  780. tag->type = MULTIBOOT_TAG_TYPE_BOOTDEV;
  781. tag->size = sizeof (struct multiboot_tag_bootdev);
  782. tag->biosdev = biosdev;
  783. tag->slice = slice;
  784. tag->part = part;
  785. ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
  786. / sizeof (grub_properly_aligned_t);
  787. }
  788. {
  789. err = retrieve_video_parameters (&ptrorig);
  790. if (err)
  791. {
  792. grub_print_error ();
  793. grub_errno = GRUB_ERR_NONE;
  794. }
  795. }
  796. #if defined (GRUB_MACHINE_EFI) && defined (__x86_64__)
  797. {
  798. struct multiboot_tag_efi64 *tag = (struct multiboot_tag_efi64 *) ptrorig;
  799. tag->type = MULTIBOOT_TAG_TYPE_EFI64;
  800. tag->size = sizeof (*tag);
  801. tag->pointer = (grub_addr_t) grub_efi_system_table;
  802. ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
  803. / sizeof (grub_properly_aligned_t);
  804. }
  805. #endif
  806. #if defined (GRUB_MACHINE_EFI) && defined (__i386__)
  807. {
  808. struct multiboot_tag_efi32 *tag = (struct multiboot_tag_efi32 *) ptrorig;
  809. tag->type = MULTIBOOT_TAG_TYPE_EFI32;
  810. tag->size = sizeof (*tag);
  811. tag->pointer = (grub_addr_t) grub_efi_system_table;
  812. ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
  813. / sizeof (grub_properly_aligned_t);
  814. }
  815. #endif
  816. #if GRUB_MACHINE_HAS_ACPI
  817. {
  818. struct multiboot_tag_old_acpi *tag = (struct multiboot_tag_old_acpi *)
  819. ptrorig;
  820. struct grub_acpi_rsdp_v10 *a = grub_acpi_get_rsdpv1 ();
  821. if (a)
  822. {
  823. tag->type = MULTIBOOT_TAG_TYPE_ACPI_OLD;
  824. tag->size = sizeof (*tag) + sizeof (*a);
  825. grub_memcpy (tag->rsdp, a, sizeof (*a));
  826. ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
  827. / sizeof (grub_properly_aligned_t);
  828. }
  829. }
  830. {
  831. struct multiboot_tag_new_acpi *tag = (struct multiboot_tag_new_acpi *)
  832. ptrorig;
  833. struct grub_acpi_rsdp_v20 *a = grub_acpi_get_rsdpv2 ();
  834. if (a)
  835. {
  836. tag->type = MULTIBOOT_TAG_TYPE_ACPI_NEW;
  837. tag->size = sizeof (*tag) + a->length;
  838. grub_memcpy (tag->rsdp, a, a->length);
  839. ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
  840. / sizeof (grub_properly_aligned_t);
  841. }
  842. }
  843. #endif
  844. #ifdef GRUB_MACHINE_EFI
  845. {
  846. struct multiboot_tag_efi_mmap *tag = (struct multiboot_tag_efi_mmap *) ptrorig;
  847. grub_efi_uintn_t efi_desc_size;
  848. grub_efi_uint32_t efi_desc_version;
  849. if (!keep_bs)
  850. {
  851. tag->type = MULTIBOOT_TAG_TYPE_EFI_MMAP;
  852. tag->size = sizeof (*tag) + efi_mmap_size;
  853. err = grub_efi_finish_boot_services (&efi_mmap_size, tag->efi_mmap, NULL,
  854. &efi_desc_size, &efi_desc_version);
  855. if (err)
  856. return err;
  857. tag->descr_size = efi_desc_size;
  858. tag->descr_vers = efi_desc_version;
  859. tag->size = sizeof (*tag) + efi_mmap_size;
  860. ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
  861. / sizeof (grub_properly_aligned_t);
  862. }
  863. }
  864. if (keep_bs)
  865. {
  866. {
  867. struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
  868. tag->type = MULTIBOOT_TAG_TYPE_EFI_BS;
  869. tag->size = sizeof (struct multiboot_tag);
  870. ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
  871. / sizeof (grub_properly_aligned_t);
  872. }
  873. #ifdef __i386__
  874. {
  875. struct multiboot_tag_efi32_ih *tag = (struct multiboot_tag_efi32_ih *) ptrorig;
  876. tag->type = MULTIBOOT_TAG_TYPE_EFI32_IH;
  877. tag->size = sizeof (struct multiboot_tag_efi32_ih);
  878. tag->pointer = (grub_addr_t) grub_efi_image_handle;
  879. ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
  880. / sizeof (grub_properly_aligned_t);
  881. }
  882. #endif
  883. #ifdef __x86_64__
  884. {
  885. struct multiboot_tag_efi64_ih *tag = (struct multiboot_tag_efi64_ih *) ptrorig;
  886. tag->type = MULTIBOOT_TAG_TYPE_EFI64_IH;
  887. tag->size = sizeof (struct multiboot_tag_efi64_ih);
  888. tag->pointer = (grub_addr_t) grub_efi_image_handle;
  889. ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
  890. / sizeof (grub_properly_aligned_t);
  891. }
  892. #endif
  893. }
  894. #endif
  895. {
  896. struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
  897. tag->type = MULTIBOOT_TAG_TYPE_END;
  898. tag->size = sizeof (struct multiboot_tag);
  899. ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
  900. / sizeof (grub_properly_aligned_t);
  901. }
  902. ((grub_uint32_t *) mbistart)[0] = (char *) ptrorig - (char *) mbistart;
  903. ((grub_uint32_t *) mbistart)[1] = 0;
  904. return GRUB_ERR_NONE;
  905. }
  906. void
  907. grub_multiboot_free_mbi (void)
  908. {
  909. struct module *cur, *next;
  910. cmdline_size = 0;
  911. total_modcmd = 0;
  912. modcnt = 0;
  913. grub_free (cmdline);
  914. cmdline = NULL;
  915. bootdev_set = 0;
  916. for (cur = modules; cur; cur = next)
  917. {
  918. next = cur->next;
  919. grub_free (cur->cmdline);
  920. grub_free (cur);
  921. }
  922. modules = NULL;
  923. modules_last = NULL;
  924. }
  925. grub_err_t
  926. grub_multiboot_init_mbi (int argc, char *argv[])
  927. {
  928. grub_ssize_t len = 0;
  929. grub_multiboot_free_mbi ();
  930. len = grub_loader_cmdline_size (argc, argv);
  931. cmdline = grub_malloc (len);
  932. if (! cmdline)
  933. return grub_errno;
  934. cmdline_size = len;
  935. grub_create_loader_cmdline (argc, argv, cmdline,
  936. cmdline_size);
  937. return GRUB_ERR_NONE;
  938. }
  939. grub_err_t
  940. grub_multiboot_add_module (grub_addr_t start, grub_size_t size,
  941. int argc, char *argv[])
  942. {
  943. struct module *newmod;
  944. grub_size_t len = 0;
  945. newmod = grub_malloc (sizeof (*newmod));
  946. if (!newmod)
  947. return grub_errno;
  948. newmod->start = start;
  949. newmod->size = size;
  950. len = grub_loader_cmdline_size (argc, argv);
  951. newmod->cmdline = grub_malloc (len);
  952. if (! newmod->cmdline)
  953. {
  954. grub_free (newmod);
  955. return grub_errno;
  956. }
  957. newmod->cmdline_size = len;
  958. total_modcmd += ALIGN_UP (len, MULTIBOOT_TAG_ALIGN);
  959. grub_create_loader_cmdline (argc, argv, newmod->cmdline,
  960. newmod->cmdline_size);
  961. if (modules_last)
  962. modules_last->next = newmod;
  963. else
  964. modules = newmod;
  965. modules_last = newmod;
  966. modcnt++;
  967. return GRUB_ERR_NONE;
  968. }
  969. void
  970. grub_multiboot_set_bootdev (void)
  971. {
  972. grub_device_t dev;
  973. slice = ~0;
  974. part = ~0;
  975. #ifdef GRUB_MACHINE_PCBIOS
  976. biosdev = grub_get_root_biosnumber ();
  977. #else
  978. biosdev = 0xffffffff;
  979. #endif
  980. if (biosdev == 0xffffffff)
  981. return;
  982. dev = grub_device_open (0);
  983. if (dev && dev->disk && dev->disk->partition)
  984. {
  985. if (dev->disk->partition->parent)
  986. {
  987. part = dev->disk->partition->number;
  988. slice = dev->disk->partition->parent->number;
  989. }
  990. else
  991. slice = dev->disk->partition->number;
  992. }
  993. if (dev)
  994. grub_device_close (dev);
  995. bootdev_set = 1;
  996. }