multiboot.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * QEMU PC System Emulator
  3. *
  4. * Copyright (c) 2003-2004 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu/option.h"
  26. #include "cpu.h"
  27. #include "hw/nvram/fw_cfg.h"
  28. #include "multiboot.h"
  29. #include "hw/loader.h"
  30. #include "elf.h"
  31. #include "sysemu/sysemu.h"
  32. #include "qemu/error-report.h"
  33. /* Show multiboot debug output */
  34. //#define DEBUG_MULTIBOOT
  35. #ifdef DEBUG_MULTIBOOT
  36. #define mb_debug(a...) error_report(a)
  37. #else
  38. #define mb_debug(a...)
  39. #endif
  40. #define MULTIBOOT_STRUCT_ADDR 0x9000
  41. #if MULTIBOOT_STRUCT_ADDR > 0xf0000
  42. #error multiboot struct needs to fit in 16 bit real mode
  43. #endif
  44. enum {
  45. /* Multiboot info */
  46. MBI_FLAGS = 0,
  47. MBI_MEM_LOWER = 4,
  48. MBI_MEM_UPPER = 8,
  49. MBI_BOOT_DEVICE = 12,
  50. MBI_CMDLINE = 16,
  51. MBI_MODS_COUNT = 20,
  52. MBI_MODS_ADDR = 24,
  53. MBI_MMAP_ADDR = 48,
  54. MBI_BOOTLOADER = 64,
  55. MBI_SIZE = 88,
  56. /* Multiboot modules */
  57. MB_MOD_START = 0,
  58. MB_MOD_END = 4,
  59. MB_MOD_CMDLINE = 8,
  60. MB_MOD_SIZE = 16,
  61. /* Region offsets */
  62. ADDR_E820_MAP = MULTIBOOT_STRUCT_ADDR + 0,
  63. ADDR_MBI = ADDR_E820_MAP + 0x500,
  64. /* Multiboot flags */
  65. MULTIBOOT_FLAGS_MEMORY = 1 << 0,
  66. MULTIBOOT_FLAGS_BOOT_DEVICE = 1 << 1,
  67. MULTIBOOT_FLAGS_CMDLINE = 1 << 2,
  68. MULTIBOOT_FLAGS_MODULES = 1 << 3,
  69. MULTIBOOT_FLAGS_MMAP = 1 << 6,
  70. MULTIBOOT_FLAGS_BOOTLOADER = 1 << 9,
  71. };
  72. typedef struct {
  73. /* buffer holding kernel, cmdlines and mb_infos */
  74. void *mb_buf;
  75. /* address in target */
  76. hwaddr mb_buf_phys;
  77. /* size of mb_buf in bytes */
  78. unsigned mb_buf_size;
  79. /* offset of mb-info's in bytes */
  80. hwaddr offset_mbinfo;
  81. /* offset in buffer for cmdlines in bytes */
  82. hwaddr offset_cmdlines;
  83. /* offset in buffer for bootloader name in bytes */
  84. hwaddr offset_bootloader;
  85. /* offset of modules in bytes */
  86. hwaddr offset_mods;
  87. /* available slots for mb modules infos */
  88. int mb_mods_avail;
  89. /* currently used slots of mb modules */
  90. int mb_mods_count;
  91. } MultibootState;
  92. const char *bootloader_name = "qemu";
  93. static uint32_t mb_add_cmdline(MultibootState *s, const char *cmdline)
  94. {
  95. hwaddr p = s->offset_cmdlines;
  96. char *b = (char *)s->mb_buf + p;
  97. memcpy(b, cmdline, strlen(cmdline) + 1);
  98. s->offset_cmdlines += strlen(b) + 1;
  99. return s->mb_buf_phys + p;
  100. }
  101. static uint32_t mb_add_bootloader(MultibootState *s, const char *bootloader)
  102. {
  103. hwaddr p = s->offset_bootloader;
  104. char *b = (char *)s->mb_buf + p;
  105. memcpy(b, bootloader, strlen(bootloader) + 1);
  106. s->offset_bootloader += strlen(b) + 1;
  107. return s->mb_buf_phys + p;
  108. }
  109. static void mb_add_mod(MultibootState *s,
  110. hwaddr start, hwaddr end,
  111. hwaddr cmdline_phys)
  112. {
  113. char *p;
  114. assert(s->mb_mods_count < s->mb_mods_avail);
  115. p = (char *)s->mb_buf + s->offset_mbinfo + MB_MOD_SIZE * s->mb_mods_count;
  116. stl_p(p + MB_MOD_START, start);
  117. stl_p(p + MB_MOD_END, end);
  118. stl_p(p + MB_MOD_CMDLINE, cmdline_phys);
  119. mb_debug("mod%02d: "TARGET_FMT_plx" - "TARGET_FMT_plx,
  120. s->mb_mods_count, start, end);
  121. s->mb_mods_count++;
  122. }
  123. int load_multiboot(X86MachineState *x86ms,
  124. FWCfgState *fw_cfg,
  125. FILE *f,
  126. const char *kernel_filename,
  127. const char *initrd_filename,
  128. const char *kernel_cmdline,
  129. int kernel_file_size,
  130. uint8_t *header)
  131. {
  132. bool multiboot_dma_enabled = X86_MACHINE_GET_CLASS(x86ms)->fwcfg_dma_enabled;
  133. int i, is_multiboot = 0;
  134. uint32_t flags = 0;
  135. uint32_t mh_entry_addr;
  136. uint32_t mh_load_addr;
  137. uint32_t mb_kernel_size;
  138. MultibootState mbs;
  139. uint8_t bootinfo[MBI_SIZE];
  140. uint8_t *mb_bootinfo_data;
  141. uint32_t cmdline_len;
  142. GList *mods = NULL;
  143. /* Ok, let's see if it is a multiboot image.
  144. The header is 12x32bit long, so the latest entry may be 8192 - 48. */
  145. for (i = 0; i < (8192 - 48); i += 4) {
  146. if (ldl_p(header+i) == 0x1BADB002) {
  147. uint32_t checksum = ldl_p(header+i+8);
  148. flags = ldl_p(header+i+4);
  149. checksum += flags;
  150. checksum += (uint32_t)0x1BADB002;
  151. if (!checksum) {
  152. is_multiboot = 1;
  153. break;
  154. }
  155. }
  156. }
  157. if (!is_multiboot)
  158. return 0; /* no multiboot */
  159. mb_debug("I believe we found a multiboot image!");
  160. memset(bootinfo, 0, sizeof(bootinfo));
  161. memset(&mbs, 0, sizeof(mbs));
  162. if (flags & 0x00000004) { /* MULTIBOOT_HEADER_HAS_VBE */
  163. error_report("multiboot knows VBE. we don't");
  164. }
  165. if (!(flags & 0x00010000)) { /* MULTIBOOT_HEADER_HAS_ADDR */
  166. uint64_t elf_entry;
  167. uint64_t elf_low, elf_high;
  168. int kernel_size;
  169. fclose(f);
  170. if (((struct elf64_hdr*)header)->e_machine == EM_X86_64) {
  171. error_report("Cannot load x86-64 image, give a 32bit one.");
  172. exit(1);
  173. }
  174. kernel_size = load_elf(kernel_filename, NULL, NULL, NULL, &elf_entry,
  175. &elf_low, &elf_high, NULL, 0, I386_ELF_MACHINE,
  176. 0, 0);
  177. if (kernel_size < 0) {
  178. error_report("Error while loading elf kernel");
  179. exit(1);
  180. }
  181. mh_load_addr = elf_low;
  182. mb_kernel_size = elf_high - elf_low;
  183. mh_entry_addr = elf_entry;
  184. mbs.mb_buf = g_malloc(mb_kernel_size);
  185. if (rom_copy(mbs.mb_buf, mh_load_addr, mb_kernel_size) != mb_kernel_size) {
  186. error_report("Error while fetching elf kernel from rom");
  187. exit(1);
  188. }
  189. mb_debug("loading multiboot-elf kernel "
  190. "(%#x bytes) with entry %#zx",
  191. mb_kernel_size, (size_t)mh_entry_addr);
  192. } else {
  193. /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_ADDR. */
  194. uint32_t mh_header_addr = ldl_p(header+i+12);
  195. uint32_t mh_load_end_addr = ldl_p(header+i+20);
  196. uint32_t mh_bss_end_addr = ldl_p(header+i+24);
  197. mh_load_addr = ldl_p(header+i+16);
  198. if (mh_header_addr < mh_load_addr) {
  199. error_report("invalid load_addr address");
  200. exit(1);
  201. }
  202. if (mh_header_addr - mh_load_addr > i) {
  203. error_report("invalid header_addr address");
  204. exit(1);
  205. }
  206. uint32_t mb_kernel_text_offset = i - (mh_header_addr - mh_load_addr);
  207. uint32_t mb_load_size = 0;
  208. mh_entry_addr = ldl_p(header+i+28);
  209. if (mh_load_end_addr) {
  210. if (mh_load_end_addr < mh_load_addr) {
  211. error_report("invalid load_end_addr address");
  212. exit(1);
  213. }
  214. mb_load_size = mh_load_end_addr - mh_load_addr;
  215. } else {
  216. if (kernel_file_size < mb_kernel_text_offset) {
  217. error_report("invalid kernel_file_size");
  218. exit(1);
  219. }
  220. mb_load_size = kernel_file_size - mb_kernel_text_offset;
  221. }
  222. if (mb_load_size > UINT32_MAX - mh_load_addr) {
  223. error_report("kernel does not fit in address space");
  224. exit(1);
  225. }
  226. if (mh_bss_end_addr) {
  227. if (mh_bss_end_addr < (mh_load_addr + mb_load_size)) {
  228. error_report("invalid bss_end_addr address");
  229. exit(1);
  230. }
  231. mb_kernel_size = mh_bss_end_addr - mh_load_addr;
  232. } else {
  233. mb_kernel_size = mb_load_size;
  234. }
  235. mb_debug("multiboot: header_addr = %#x", mh_header_addr);
  236. mb_debug("multiboot: load_addr = %#x", mh_load_addr);
  237. mb_debug("multiboot: load_end_addr = %#x", mh_load_end_addr);
  238. mb_debug("multiboot: bss_end_addr = %#x", mh_bss_end_addr);
  239. mb_debug("loading multiboot kernel (%#x bytes) at %#x",
  240. mb_load_size, mh_load_addr);
  241. mbs.mb_buf = g_malloc(mb_kernel_size);
  242. fseek(f, mb_kernel_text_offset, SEEK_SET);
  243. if (fread(mbs.mb_buf, 1, mb_load_size, f) != mb_load_size) {
  244. error_report("fread() failed");
  245. exit(1);
  246. }
  247. memset(mbs.mb_buf + mb_load_size, 0, mb_kernel_size - mb_load_size);
  248. fclose(f);
  249. }
  250. mbs.mb_buf_phys = mh_load_addr;
  251. mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_kernel_size);
  252. mbs.offset_mbinfo = mbs.mb_buf_size;
  253. /* Calculate space for cmdlines, bootloader name, and mb_mods */
  254. cmdline_len = strlen(kernel_filename) + 1;
  255. cmdline_len += strlen(kernel_cmdline) + 1;
  256. if (initrd_filename) {
  257. const char *r = initrd_filename;
  258. cmdline_len += strlen(initrd_filename) + 1;
  259. while (*r) {
  260. char *value;
  261. r = get_opt_value(r, &value);
  262. mbs.mb_mods_avail++;
  263. mods = g_list_append(mods, value);
  264. if (*r) {
  265. r++;
  266. }
  267. }
  268. }
  269. mbs.mb_buf_size += cmdline_len;
  270. mbs.mb_buf_size += MB_MOD_SIZE * mbs.mb_mods_avail;
  271. mbs.mb_buf_size += strlen(bootloader_name) + 1;
  272. mbs.mb_buf_size = TARGET_PAGE_ALIGN(mbs.mb_buf_size);
  273. /* enlarge mb_buf to hold cmdlines, bootloader, mb-info structs */
  274. mbs.mb_buf = g_realloc(mbs.mb_buf, mbs.mb_buf_size);
  275. mbs.offset_cmdlines = mbs.offset_mbinfo + mbs.mb_mods_avail * MB_MOD_SIZE;
  276. mbs.offset_bootloader = mbs.offset_cmdlines + cmdline_len;
  277. if (mods) {
  278. GList *tmpl = mods;
  279. mbs.offset_mods = mbs.mb_buf_size;
  280. while (tmpl) {
  281. char *next_space;
  282. int mb_mod_length;
  283. uint32_t offs = mbs.mb_buf_size;
  284. char *one_file = tmpl->data;
  285. /* if a space comes after the module filename, treat everything
  286. after that as parameters */
  287. hwaddr c = mb_add_cmdline(&mbs, one_file);
  288. next_space = strchr(one_file, ' ');
  289. if (next_space) {
  290. *next_space = '\0';
  291. }
  292. mb_debug("multiboot loading module: %s", one_file);
  293. mb_mod_length = get_image_size(one_file);
  294. if (mb_mod_length < 0) {
  295. error_report("Failed to open file '%s'", one_file);
  296. exit(1);
  297. }
  298. mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_mod_length + mbs.mb_buf_size);
  299. mbs.mb_buf = g_realloc(mbs.mb_buf, mbs.mb_buf_size);
  300. if (load_image_size(one_file, (unsigned char *)mbs.mb_buf + offs,
  301. mbs.mb_buf_size - offs) < 0) {
  302. error_report("Error loading file '%s'", one_file);
  303. exit(1);
  304. }
  305. mb_add_mod(&mbs, mbs.mb_buf_phys + offs,
  306. mbs.mb_buf_phys + offs + mb_mod_length, c);
  307. mb_debug("mod_start: %p\nmod_end: %p\n cmdline: "TARGET_FMT_plx,
  308. (char *)mbs.mb_buf + offs,
  309. (char *)mbs.mb_buf + offs + mb_mod_length, c);
  310. g_free(one_file);
  311. tmpl = tmpl->next;
  312. }
  313. g_list_free(mods);
  314. }
  315. /* Commandline support */
  316. char kcmdline[strlen(kernel_filename) + strlen(kernel_cmdline) + 2];
  317. snprintf(kcmdline, sizeof(kcmdline), "%s %s",
  318. kernel_filename, kernel_cmdline);
  319. stl_p(bootinfo + MBI_CMDLINE, mb_add_cmdline(&mbs, kcmdline));
  320. stl_p(bootinfo + MBI_BOOTLOADER, mb_add_bootloader(&mbs, bootloader_name));
  321. stl_p(bootinfo + MBI_MODS_ADDR, mbs.mb_buf_phys + mbs.offset_mbinfo);
  322. stl_p(bootinfo + MBI_MODS_COUNT, mbs.mb_mods_count); /* mods_count */
  323. /* the kernel is where we want it to be now */
  324. stl_p(bootinfo + MBI_FLAGS, MULTIBOOT_FLAGS_MEMORY
  325. | MULTIBOOT_FLAGS_BOOT_DEVICE
  326. | MULTIBOOT_FLAGS_CMDLINE
  327. | MULTIBOOT_FLAGS_MODULES
  328. | MULTIBOOT_FLAGS_MMAP
  329. | MULTIBOOT_FLAGS_BOOTLOADER);
  330. stl_p(bootinfo + MBI_BOOT_DEVICE, 0x8000ffff); /* XXX: use the -boot switch? */
  331. stl_p(bootinfo + MBI_MMAP_ADDR, ADDR_E820_MAP);
  332. mb_debug("multiboot: entry_addr = %#x", mh_entry_addr);
  333. mb_debug(" mb_buf_phys = "TARGET_FMT_plx, mbs.mb_buf_phys);
  334. mb_debug(" mod_start = "TARGET_FMT_plx,
  335. mbs.mb_buf_phys + mbs.offset_mods);
  336. mb_debug(" mb_mods_count = %d", mbs.mb_mods_count);
  337. /* save bootinfo off the stack */
  338. mb_bootinfo_data = g_memdup(bootinfo, sizeof(bootinfo));
  339. /* Pass variables to option rom */
  340. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ENTRY, mh_entry_addr);
  341. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_load_addr);
  342. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, mbs.mb_buf_size);
  343. fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA,
  344. mbs.mb_buf, mbs.mb_buf_size);
  345. fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, ADDR_MBI);
  346. fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, sizeof(bootinfo));
  347. fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, mb_bootinfo_data,
  348. sizeof(bootinfo));
  349. if (multiboot_dma_enabled) {
  350. option_rom[nb_option_roms].name = "multiboot_dma.bin";
  351. } else {
  352. option_rom[nb_option_roms].name = "multiboot.bin";
  353. }
  354. option_rom[nb_option_roms].bootindex = 0;
  355. nb_option_roms++;
  356. return 1; /* yes, we are multiboot */
  357. }