multiboot.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 "hw.h"
  25. #include "fw_cfg.h"
  26. #include "multiboot.h"
  27. #include "loader.h"
  28. #include "elf.h"
  29. #include "sysemu/sysemu.h"
  30. /* Show multiboot debug output */
  31. //#define DEBUG_MULTIBOOT
  32. #ifdef DEBUG_MULTIBOOT
  33. #define mb_debug(a...) fprintf(stderr, ## a)
  34. #else
  35. #define mb_debug(a...)
  36. #endif
  37. #define MULTIBOOT_STRUCT_ADDR 0x9000
  38. #if MULTIBOOT_STRUCT_ADDR > 0xf0000
  39. #error multiboot struct needs to fit in 16 bit real mode
  40. #endif
  41. enum {
  42. /* Multiboot info */
  43. MBI_FLAGS = 0,
  44. MBI_MEM_LOWER = 4,
  45. MBI_MEM_UPPER = 8,
  46. MBI_BOOT_DEVICE = 12,
  47. MBI_CMDLINE = 16,
  48. MBI_MODS_COUNT = 20,
  49. MBI_MODS_ADDR = 24,
  50. MBI_MMAP_ADDR = 48,
  51. MBI_SIZE = 88,
  52. /* Multiboot modules */
  53. MB_MOD_START = 0,
  54. MB_MOD_END = 4,
  55. MB_MOD_CMDLINE = 8,
  56. MB_MOD_SIZE = 16,
  57. /* Region offsets */
  58. ADDR_E820_MAP = MULTIBOOT_STRUCT_ADDR + 0,
  59. ADDR_MBI = ADDR_E820_MAP + 0x500,
  60. /* Multiboot flags */
  61. MULTIBOOT_FLAGS_MEMORY = 1 << 0,
  62. MULTIBOOT_FLAGS_BOOT_DEVICE = 1 << 1,
  63. MULTIBOOT_FLAGS_CMDLINE = 1 << 2,
  64. MULTIBOOT_FLAGS_MODULES = 1 << 3,
  65. MULTIBOOT_FLAGS_MMAP = 1 << 6,
  66. };
  67. typedef struct {
  68. /* buffer holding kernel, cmdlines and mb_infos */
  69. void *mb_buf;
  70. /* address in target */
  71. hwaddr mb_buf_phys;
  72. /* size of mb_buf in bytes */
  73. unsigned mb_buf_size;
  74. /* offset of mb-info's in bytes */
  75. hwaddr offset_mbinfo;
  76. /* offset in buffer for cmdlines in bytes */
  77. hwaddr offset_cmdlines;
  78. /* offset of modules in bytes */
  79. hwaddr offset_mods;
  80. /* available slots for mb modules infos */
  81. int mb_mods_avail;
  82. /* currently used slots of mb modules */
  83. int mb_mods_count;
  84. } MultibootState;
  85. static uint32_t mb_add_cmdline(MultibootState *s, const char *cmdline)
  86. {
  87. hwaddr p = s->offset_cmdlines;
  88. char *b = (char *)s->mb_buf + p;
  89. get_opt_value(b, strlen(cmdline) + 1, cmdline);
  90. s->offset_cmdlines += strlen(b) + 1;
  91. return s->mb_buf_phys + p;
  92. }
  93. static void mb_add_mod(MultibootState *s,
  94. hwaddr start, hwaddr end,
  95. hwaddr cmdline_phys)
  96. {
  97. char *p;
  98. assert(s->mb_mods_count < s->mb_mods_avail);
  99. p = (char *)s->mb_buf + s->offset_mbinfo + MB_MOD_SIZE * s->mb_mods_count;
  100. stl_p(p + MB_MOD_START, start);
  101. stl_p(p + MB_MOD_END, end);
  102. stl_p(p + MB_MOD_CMDLINE, cmdline_phys);
  103. mb_debug("mod%02d: "TARGET_FMT_plx" - "TARGET_FMT_plx"\n",
  104. s->mb_mods_count, start, end);
  105. s->mb_mods_count++;
  106. }
  107. int load_multiboot(void *fw_cfg,
  108. FILE *f,
  109. const char *kernel_filename,
  110. const char *initrd_filename,
  111. const char *kernel_cmdline,
  112. int kernel_file_size,
  113. uint8_t *header)
  114. {
  115. int i, is_multiboot = 0;
  116. uint32_t flags = 0;
  117. uint32_t mh_entry_addr;
  118. uint32_t mh_load_addr;
  119. uint32_t mb_kernel_size;
  120. MultibootState mbs;
  121. uint8_t bootinfo[MBI_SIZE];
  122. uint8_t *mb_bootinfo_data;
  123. /* Ok, let's see if it is a multiboot image.
  124. The header is 12x32bit long, so the latest entry may be 8192 - 48. */
  125. for (i = 0; i < (8192 - 48); i += 4) {
  126. if (ldl_p(header+i) == 0x1BADB002) {
  127. uint32_t checksum = ldl_p(header+i+8);
  128. flags = ldl_p(header+i+4);
  129. checksum += flags;
  130. checksum += (uint32_t)0x1BADB002;
  131. if (!checksum) {
  132. is_multiboot = 1;
  133. break;
  134. }
  135. }
  136. }
  137. if (!is_multiboot)
  138. return 0; /* no multiboot */
  139. mb_debug("qemu: I believe we found a multiboot image!\n");
  140. memset(bootinfo, 0, sizeof(bootinfo));
  141. memset(&mbs, 0, sizeof(mbs));
  142. if (flags & 0x00000004) { /* MULTIBOOT_HEADER_HAS_VBE */
  143. fprintf(stderr, "qemu: multiboot knows VBE. we don't.\n");
  144. }
  145. if (!(flags & 0x00010000)) { /* MULTIBOOT_HEADER_HAS_ADDR */
  146. uint64_t elf_entry;
  147. uint64_t elf_low, elf_high;
  148. int kernel_size;
  149. fclose(f);
  150. if (((struct elf64_hdr*)header)->e_machine == EM_X86_64) {
  151. fprintf(stderr, "Cannot load x86-64 image, give a 32bit one.\n");
  152. exit(1);
  153. }
  154. kernel_size = load_elf(kernel_filename, NULL, NULL, &elf_entry,
  155. &elf_low, &elf_high, 0, ELF_MACHINE, 0);
  156. if (kernel_size < 0) {
  157. fprintf(stderr, "Error while loading elf kernel\n");
  158. exit(1);
  159. }
  160. mh_load_addr = elf_low;
  161. mb_kernel_size = elf_high - elf_low;
  162. mh_entry_addr = elf_entry;
  163. mbs.mb_buf = g_malloc(mb_kernel_size);
  164. if (rom_copy(mbs.mb_buf, mh_load_addr, mb_kernel_size) != mb_kernel_size) {
  165. fprintf(stderr, "Error while fetching elf kernel from rom\n");
  166. exit(1);
  167. }
  168. mb_debug("qemu: loading multiboot-elf kernel (%#x bytes) with entry %#zx\n",
  169. mb_kernel_size, (size_t)mh_entry_addr);
  170. } else {
  171. /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_ADDR. */
  172. uint32_t mh_header_addr = ldl_p(header+i+12);
  173. uint32_t mh_load_end_addr = ldl_p(header+i+20);
  174. uint32_t mh_bss_end_addr = ldl_p(header+i+24);
  175. mh_load_addr = ldl_p(header+i+16);
  176. uint32_t mb_kernel_text_offset = i - (mh_header_addr - mh_load_addr);
  177. uint32_t mb_load_size = 0;
  178. mh_entry_addr = ldl_p(header+i+28);
  179. if (mh_load_end_addr) {
  180. mb_kernel_size = mh_bss_end_addr - mh_load_addr;
  181. mb_load_size = mh_load_end_addr - mh_load_addr;
  182. } else {
  183. mb_kernel_size = kernel_file_size - mb_kernel_text_offset;
  184. mb_load_size = mb_kernel_size;
  185. }
  186. /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_VBE.
  187. uint32_t mh_mode_type = ldl_p(header+i+32);
  188. uint32_t mh_width = ldl_p(header+i+36);
  189. uint32_t mh_height = ldl_p(header+i+40);
  190. uint32_t mh_depth = ldl_p(header+i+44); */
  191. mb_debug("multiboot: mh_header_addr = %#x\n", mh_header_addr);
  192. mb_debug("multiboot: mh_load_addr = %#x\n", mh_load_addr);
  193. mb_debug("multiboot: mh_load_end_addr = %#x\n", mh_load_end_addr);
  194. mb_debug("multiboot: mh_bss_end_addr = %#x\n", mh_bss_end_addr);
  195. mb_debug("qemu: loading multiboot kernel (%#x bytes) at %#x\n",
  196. mb_load_size, mh_load_addr);
  197. mbs.mb_buf = g_malloc(mb_kernel_size);
  198. fseek(f, mb_kernel_text_offset, SEEK_SET);
  199. if (fread(mbs.mb_buf, 1, mb_load_size, f) != mb_load_size) {
  200. fprintf(stderr, "fread() failed\n");
  201. exit(1);
  202. }
  203. memset(mbs.mb_buf + mb_load_size, 0, mb_kernel_size - mb_load_size);
  204. fclose(f);
  205. }
  206. mbs.mb_buf_phys = mh_load_addr;
  207. mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_kernel_size);
  208. mbs.offset_mbinfo = mbs.mb_buf_size;
  209. /* Calculate space for cmdlines and mb_mods */
  210. mbs.mb_buf_size += strlen(kernel_filename) + 1;
  211. mbs.mb_buf_size += strlen(kernel_cmdline) + 1;
  212. if (initrd_filename) {
  213. const char *r = initrd_filename;
  214. mbs.mb_buf_size += strlen(r) + 1;
  215. mbs.mb_mods_avail = 1;
  216. while (*(r = get_opt_value(NULL, 0, r))) {
  217. mbs.mb_mods_avail++;
  218. r++;
  219. }
  220. mbs.mb_buf_size += MB_MOD_SIZE * mbs.mb_mods_avail;
  221. }
  222. mbs.mb_buf_size = TARGET_PAGE_ALIGN(mbs.mb_buf_size);
  223. /* enlarge mb_buf to hold cmdlines and mb-info structs */
  224. mbs.mb_buf = g_realloc(mbs.mb_buf, mbs.mb_buf_size);
  225. mbs.offset_cmdlines = mbs.offset_mbinfo + mbs.mb_mods_avail * MB_MOD_SIZE;
  226. if (initrd_filename) {
  227. char *next_initrd, not_last;
  228. mbs.offset_mods = mbs.mb_buf_size;
  229. do {
  230. char *next_space;
  231. int mb_mod_length;
  232. uint32_t offs = mbs.mb_buf_size;
  233. next_initrd = (char *)get_opt_value(NULL, 0, initrd_filename);
  234. not_last = *next_initrd;
  235. *next_initrd = '\0';
  236. /* if a space comes after the module filename, treat everything
  237. after that as parameters */
  238. hwaddr c = mb_add_cmdline(&mbs, initrd_filename);
  239. if ((next_space = strchr(initrd_filename, ' ')))
  240. *next_space = '\0';
  241. mb_debug("multiboot loading module: %s\n", initrd_filename);
  242. mb_mod_length = get_image_size(initrd_filename);
  243. if (mb_mod_length < 0) {
  244. fprintf(stderr, "Failed to open file '%s'\n", initrd_filename);
  245. exit(1);
  246. }
  247. mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_mod_length + mbs.mb_buf_size);
  248. mbs.mb_buf = g_realloc(mbs.mb_buf, mbs.mb_buf_size);
  249. load_image(initrd_filename, (unsigned char *)mbs.mb_buf + offs);
  250. mb_add_mod(&mbs, mbs.mb_buf_phys + offs,
  251. mbs.mb_buf_phys + offs + mb_mod_length, c);
  252. mb_debug("mod_start: %p\nmod_end: %p\n cmdline: "TARGET_FMT_plx"\n",
  253. (char *)mbs.mb_buf + offs,
  254. (char *)mbs.mb_buf + offs + mb_mod_length, c);
  255. initrd_filename = next_initrd+1;
  256. } while (not_last);
  257. }
  258. /* Commandline support */
  259. char kcmdline[strlen(kernel_filename) + strlen(kernel_cmdline) + 2];
  260. snprintf(kcmdline, sizeof(kcmdline), "%s %s",
  261. kernel_filename, kernel_cmdline);
  262. stl_p(bootinfo + MBI_CMDLINE, mb_add_cmdline(&mbs, kcmdline));
  263. stl_p(bootinfo + MBI_MODS_ADDR, mbs.mb_buf_phys + mbs.offset_mbinfo);
  264. stl_p(bootinfo + MBI_MODS_COUNT, mbs.mb_mods_count); /* mods_count */
  265. /* the kernel is where we want it to be now */
  266. stl_p(bootinfo + MBI_FLAGS, MULTIBOOT_FLAGS_MEMORY
  267. | MULTIBOOT_FLAGS_BOOT_DEVICE
  268. | MULTIBOOT_FLAGS_CMDLINE
  269. | MULTIBOOT_FLAGS_MODULES
  270. | MULTIBOOT_FLAGS_MMAP);
  271. stl_p(bootinfo + MBI_MEM_LOWER, 640);
  272. stl_p(bootinfo + MBI_MEM_UPPER, (ram_size / 1024) - 1024);
  273. stl_p(bootinfo + MBI_BOOT_DEVICE, 0x8000ffff); /* XXX: use the -boot switch? */
  274. stl_p(bootinfo + MBI_MMAP_ADDR, ADDR_E820_MAP);
  275. mb_debug("multiboot: mh_entry_addr = %#x\n", mh_entry_addr);
  276. mb_debug(" mb_buf_phys = "TARGET_FMT_plx"\n", mbs.mb_buf_phys);
  277. mb_debug(" mod_start = "TARGET_FMT_plx"\n", mbs.mb_buf_phys + mbs.offset_mods);
  278. mb_debug(" mb_mods_count = %d\n", mbs.mb_mods_count);
  279. /* save bootinfo off the stack */
  280. mb_bootinfo_data = g_malloc(sizeof(bootinfo));
  281. memcpy(mb_bootinfo_data, bootinfo, sizeof(bootinfo));
  282. /* Pass variables to option rom */
  283. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ENTRY, mh_entry_addr);
  284. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_load_addr);
  285. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, mbs.mb_buf_size);
  286. fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA,
  287. mbs.mb_buf, mbs.mb_buf_size);
  288. fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, ADDR_MBI);
  289. fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, sizeof(bootinfo));
  290. fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, mb_bootinfo_data,
  291. sizeof(bootinfo));
  292. option_rom[nb_option_roms].name = "multiboot.bin";
  293. option_rom[nb_option_roms].bootindex = 0;
  294. nb_option_roms++;
  295. return 1; /* yes, we are multiboot */
  296. }