kexec-bzimage64.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * Kexec bzImage loader
  3. *
  4. * Copyright (C) 2014 Red Hat Inc.
  5. * Authors:
  6. * Vivek Goyal <vgoyal@redhat.com>
  7. *
  8. * This source code is licensed under the GNU General Public License,
  9. * Version 2. See the file COPYING for more details.
  10. */
  11. #define pr_fmt(fmt) "kexec-bzImage64: " fmt
  12. #include <linux/string.h>
  13. #include <linux/printk.h>
  14. #include <linux/errno.h>
  15. #include <linux/slab.h>
  16. #include <linux/kexec.h>
  17. #include <linux/kernel.h>
  18. #include <linux/mm.h>
  19. #include <linux/efi.h>
  20. #include <linux/verification.h>
  21. #include <asm/bootparam.h>
  22. #include <asm/setup.h>
  23. #include <asm/crash.h>
  24. #include <asm/efi.h>
  25. #include <asm/kexec-bzimage64.h>
  26. #define MAX_ELFCOREHDR_STR_LEN 30 /* elfcorehdr=0x<64bit-value> */
  27. /*
  28. * Defines lowest physical address for various segments. Not sure where
  29. * exactly these limits came from. Current bzimage64 loader in kexec-tools
  30. * uses these so I am retaining it. It can be changed over time as we gain
  31. * more insight.
  32. */
  33. #define MIN_PURGATORY_ADDR 0x3000
  34. #define MIN_BOOTPARAM_ADDR 0x3000
  35. #define MIN_KERNEL_LOAD_ADDR 0x100000
  36. #define MIN_INITRD_LOAD_ADDR 0x1000000
  37. /*
  38. * This is a place holder for all boot loader specific data structure which
  39. * gets allocated in one call but gets freed much later during cleanup
  40. * time. Right now there is only one field but it can grow as need be.
  41. */
  42. struct bzimage64_data {
  43. /*
  44. * Temporary buffer to hold bootparams buffer. This should be
  45. * freed once the bootparam segment has been loaded.
  46. */
  47. void *bootparams_buf;
  48. };
  49. static int setup_initrd(struct boot_params *params,
  50. unsigned long initrd_load_addr, unsigned long initrd_len)
  51. {
  52. params->hdr.ramdisk_image = initrd_load_addr & 0xffffffffUL;
  53. params->hdr.ramdisk_size = initrd_len & 0xffffffffUL;
  54. params->ext_ramdisk_image = initrd_load_addr >> 32;
  55. params->ext_ramdisk_size = initrd_len >> 32;
  56. return 0;
  57. }
  58. static int setup_cmdline(struct kimage *image, struct boot_params *params,
  59. unsigned long bootparams_load_addr,
  60. unsigned long cmdline_offset, char *cmdline,
  61. unsigned long cmdline_len)
  62. {
  63. char *cmdline_ptr = ((char *)params) + cmdline_offset;
  64. unsigned long cmdline_ptr_phys, len = 0;
  65. uint32_t cmdline_low_32, cmdline_ext_32;
  66. if (image->type == KEXEC_TYPE_CRASH) {
  67. len = sprintf(cmdline_ptr,
  68. "elfcorehdr=0x%lx ", image->arch.elf_load_addr);
  69. }
  70. memcpy(cmdline_ptr + len, cmdline, cmdline_len);
  71. cmdline_len += len;
  72. cmdline_ptr[cmdline_len - 1] = '\0';
  73. pr_debug("Final command line is: %s\n", cmdline_ptr);
  74. cmdline_ptr_phys = bootparams_load_addr + cmdline_offset;
  75. cmdline_low_32 = cmdline_ptr_phys & 0xffffffffUL;
  76. cmdline_ext_32 = cmdline_ptr_phys >> 32;
  77. params->hdr.cmd_line_ptr = cmdline_low_32;
  78. if (cmdline_ext_32)
  79. params->ext_cmd_line_ptr = cmdline_ext_32;
  80. return 0;
  81. }
  82. static int setup_e820_entries(struct boot_params *params)
  83. {
  84. unsigned int nr_e820_entries;
  85. nr_e820_entries = e820_saved->nr_map;
  86. /* TODO: Pass entries more than E820MAX in bootparams setup data */
  87. if (nr_e820_entries > E820MAX)
  88. nr_e820_entries = E820MAX;
  89. params->e820_entries = nr_e820_entries;
  90. memcpy(&params->e820_map, &e820_saved->map,
  91. nr_e820_entries * sizeof(struct e820entry));
  92. return 0;
  93. }
  94. #ifdef CONFIG_EFI
  95. static int setup_efi_info_memmap(struct boot_params *params,
  96. unsigned long params_load_addr,
  97. unsigned int efi_map_offset,
  98. unsigned int efi_map_sz)
  99. {
  100. void *efi_map = (void *)params + efi_map_offset;
  101. unsigned long efi_map_phys_addr = params_load_addr + efi_map_offset;
  102. struct efi_info *ei = &params->efi_info;
  103. if (!efi_map_sz)
  104. return 0;
  105. efi_runtime_map_copy(efi_map, efi_map_sz);
  106. ei->efi_memmap = efi_map_phys_addr & 0xffffffff;
  107. ei->efi_memmap_hi = efi_map_phys_addr >> 32;
  108. ei->efi_memmap_size = efi_map_sz;
  109. return 0;
  110. }
  111. static int
  112. prepare_add_efi_setup_data(struct boot_params *params,
  113. unsigned long params_load_addr,
  114. unsigned int efi_setup_data_offset)
  115. {
  116. unsigned long setup_data_phys;
  117. struct setup_data *sd = (void *)params + efi_setup_data_offset;
  118. struct efi_setup_data *esd = (void *)sd + sizeof(struct setup_data);
  119. esd->fw_vendor = efi.fw_vendor;
  120. esd->runtime = efi.runtime;
  121. esd->tables = efi.config_table;
  122. esd->smbios = efi.smbios;
  123. sd->type = SETUP_EFI;
  124. sd->len = sizeof(struct efi_setup_data);
  125. /* Add setup data */
  126. setup_data_phys = params_load_addr + efi_setup_data_offset;
  127. sd->next = params->hdr.setup_data;
  128. params->hdr.setup_data = setup_data_phys;
  129. return 0;
  130. }
  131. static int
  132. setup_efi_state(struct boot_params *params, unsigned long params_load_addr,
  133. unsigned int efi_map_offset, unsigned int efi_map_sz,
  134. unsigned int efi_setup_data_offset)
  135. {
  136. struct efi_info *current_ei = &boot_params.efi_info;
  137. struct efi_info *ei = &params->efi_info;
  138. if (!current_ei->efi_memmap_size)
  139. return 0;
  140. /*
  141. * If 1:1 mapping is not enabled, second kernel can not setup EFI
  142. * and use EFI run time services. User space will have to pass
  143. * acpi_rsdp=<addr> on kernel command line to make second kernel boot
  144. * without efi.
  145. */
  146. if (efi_enabled(EFI_OLD_MEMMAP))
  147. return 0;
  148. ei->efi_loader_signature = current_ei->efi_loader_signature;
  149. ei->efi_systab = current_ei->efi_systab;
  150. ei->efi_systab_hi = current_ei->efi_systab_hi;
  151. ei->efi_memdesc_version = current_ei->efi_memdesc_version;
  152. ei->efi_memdesc_size = efi_get_runtime_map_desc_size();
  153. setup_efi_info_memmap(params, params_load_addr, efi_map_offset,
  154. efi_map_sz);
  155. prepare_add_efi_setup_data(params, params_load_addr,
  156. efi_setup_data_offset);
  157. return 0;
  158. }
  159. #endif /* CONFIG_EFI */
  160. static int
  161. setup_boot_parameters(struct kimage *image, struct boot_params *params,
  162. unsigned long params_load_addr,
  163. unsigned int efi_map_offset, unsigned int efi_map_sz,
  164. unsigned int efi_setup_data_offset)
  165. {
  166. unsigned int nr_e820_entries;
  167. unsigned long long mem_k, start, end;
  168. int i, ret = 0;
  169. /* Get subarch from existing bootparams */
  170. params->hdr.hardware_subarch = boot_params.hdr.hardware_subarch;
  171. /* Copying screen_info will do? */
  172. memcpy(&params->screen_info, &boot_params.screen_info,
  173. sizeof(struct screen_info));
  174. /* Fill in memsize later */
  175. params->screen_info.ext_mem_k = 0;
  176. params->alt_mem_k = 0;
  177. /* Default APM info */
  178. memset(&params->apm_bios_info, 0, sizeof(params->apm_bios_info));
  179. /* Default drive info */
  180. memset(&params->hd0_info, 0, sizeof(params->hd0_info));
  181. memset(&params->hd1_info, 0, sizeof(params->hd1_info));
  182. if (image->type == KEXEC_TYPE_CRASH) {
  183. ret = crash_setup_memmap_entries(image, params);
  184. if (ret)
  185. return ret;
  186. } else
  187. setup_e820_entries(params);
  188. nr_e820_entries = params->e820_entries;
  189. for (i = 0; i < nr_e820_entries; i++) {
  190. if (params->e820_map[i].type != E820_RAM)
  191. continue;
  192. start = params->e820_map[i].addr;
  193. end = params->e820_map[i].addr + params->e820_map[i].size - 1;
  194. if ((start <= 0x100000) && end > 0x100000) {
  195. mem_k = (end >> 10) - (0x100000 >> 10);
  196. params->screen_info.ext_mem_k = mem_k;
  197. params->alt_mem_k = mem_k;
  198. if (mem_k > 0xfc00)
  199. params->screen_info.ext_mem_k = 0xfc00; /* 64M*/
  200. if (mem_k > 0xffffffff)
  201. params->alt_mem_k = 0xffffffff;
  202. }
  203. }
  204. #ifdef CONFIG_EFI
  205. /* Setup EFI state */
  206. setup_efi_state(params, params_load_addr, efi_map_offset, efi_map_sz,
  207. efi_setup_data_offset);
  208. #endif
  209. /* Setup EDD info */
  210. memcpy(params->eddbuf, boot_params.eddbuf,
  211. EDDMAXNR * sizeof(struct edd_info));
  212. params->eddbuf_entries = boot_params.eddbuf_entries;
  213. memcpy(params->edd_mbr_sig_buffer, boot_params.edd_mbr_sig_buffer,
  214. EDD_MBR_SIG_MAX * sizeof(unsigned int));
  215. return ret;
  216. }
  217. static int bzImage64_probe(const char *buf, unsigned long len)
  218. {
  219. int ret = -ENOEXEC;
  220. struct setup_header *header;
  221. /* kernel should be at least two sectors long */
  222. if (len < 2 * 512) {
  223. pr_err("File is too short to be a bzImage\n");
  224. return ret;
  225. }
  226. header = (struct setup_header *)(buf + offsetof(struct boot_params, hdr));
  227. if (memcmp((char *)&header->header, "HdrS", 4) != 0) {
  228. pr_err("Not a bzImage\n");
  229. return ret;
  230. }
  231. if (header->boot_flag != 0xAA55) {
  232. pr_err("No x86 boot sector present\n");
  233. return ret;
  234. }
  235. if (header->version < 0x020C) {
  236. pr_err("Must be at least protocol version 2.12\n");
  237. return ret;
  238. }
  239. if (!(header->loadflags & LOADED_HIGH)) {
  240. pr_err("zImage not a bzImage\n");
  241. return ret;
  242. }
  243. if (!(header->xloadflags & XLF_KERNEL_64)) {
  244. pr_err("Not a bzImage64. XLF_KERNEL_64 is not set.\n");
  245. return ret;
  246. }
  247. if (!(header->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G)) {
  248. pr_err("XLF_CAN_BE_LOADED_ABOVE_4G is not set.\n");
  249. return ret;
  250. }
  251. /*
  252. * Can't handle 32bit EFI as it does not allow loading kernel
  253. * above 4G. This should be handled by 32bit bzImage loader
  254. */
  255. if (efi_enabled(EFI_RUNTIME_SERVICES) && !efi_enabled(EFI_64BIT)) {
  256. pr_debug("EFI is 32 bit. Can't load kernel above 4G.\n");
  257. return ret;
  258. }
  259. /* I've got a bzImage */
  260. pr_debug("It's a relocatable bzImage64\n");
  261. ret = 0;
  262. return ret;
  263. }
  264. static void *bzImage64_load(struct kimage *image, char *kernel,
  265. unsigned long kernel_len, char *initrd,
  266. unsigned long initrd_len, char *cmdline,
  267. unsigned long cmdline_len)
  268. {
  269. struct setup_header *header;
  270. int setup_sects, kern16_size, ret = 0;
  271. unsigned long setup_header_size, params_cmdline_sz, params_misc_sz;
  272. struct boot_params *params;
  273. unsigned long bootparam_load_addr, kernel_load_addr, initrd_load_addr;
  274. unsigned long purgatory_load_addr;
  275. unsigned long kernel_bufsz, kernel_memsz, kernel_align;
  276. char *kernel_buf;
  277. struct bzimage64_data *ldata;
  278. struct kexec_entry64_regs regs64;
  279. void *stack;
  280. unsigned int setup_hdr_offset = offsetof(struct boot_params, hdr);
  281. unsigned int efi_map_offset, efi_map_sz, efi_setup_data_offset;
  282. header = (struct setup_header *)(kernel + setup_hdr_offset);
  283. setup_sects = header->setup_sects;
  284. if (setup_sects == 0)
  285. setup_sects = 4;
  286. kern16_size = (setup_sects + 1) * 512;
  287. if (kernel_len < kern16_size) {
  288. pr_err("bzImage truncated\n");
  289. return ERR_PTR(-ENOEXEC);
  290. }
  291. if (cmdline_len > header->cmdline_size) {
  292. pr_err("Kernel command line too long\n");
  293. return ERR_PTR(-EINVAL);
  294. }
  295. /*
  296. * In case of crash dump, we will append elfcorehdr=<addr> to
  297. * command line. Make sure it does not overflow
  298. */
  299. if (cmdline_len + MAX_ELFCOREHDR_STR_LEN > header->cmdline_size) {
  300. pr_debug("Appending elfcorehdr=<addr> to command line exceeds maximum allowed length\n");
  301. return ERR_PTR(-EINVAL);
  302. }
  303. /* Allocate and load backup region */
  304. if (image->type == KEXEC_TYPE_CRASH) {
  305. ret = crash_load_segments(image);
  306. if (ret)
  307. return ERR_PTR(ret);
  308. }
  309. /*
  310. * Load purgatory. For 64bit entry point, purgatory code can be
  311. * anywhere.
  312. */
  313. ret = kexec_load_purgatory(image, MIN_PURGATORY_ADDR, ULONG_MAX, 1,
  314. &purgatory_load_addr);
  315. if (ret) {
  316. pr_err("Loading purgatory failed\n");
  317. return ERR_PTR(ret);
  318. }
  319. pr_debug("Loaded purgatory at 0x%lx\n", purgatory_load_addr);
  320. /*
  321. * Load Bootparams and cmdline and space for efi stuff.
  322. *
  323. * Allocate memory together for multiple data structures so
  324. * that they all can go in single area/segment and we don't
  325. * have to create separate segment for each. Keeps things
  326. * little bit simple
  327. */
  328. efi_map_sz = efi_get_runtime_map_size();
  329. efi_map_sz = ALIGN(efi_map_sz, 16);
  330. params_cmdline_sz = sizeof(struct boot_params) + cmdline_len +
  331. MAX_ELFCOREHDR_STR_LEN;
  332. params_cmdline_sz = ALIGN(params_cmdline_sz, 16);
  333. params_misc_sz = params_cmdline_sz + efi_map_sz +
  334. sizeof(struct setup_data) +
  335. sizeof(struct efi_setup_data);
  336. params = kzalloc(params_misc_sz, GFP_KERNEL);
  337. if (!params)
  338. return ERR_PTR(-ENOMEM);
  339. efi_map_offset = params_cmdline_sz;
  340. efi_setup_data_offset = efi_map_offset + efi_map_sz;
  341. /* Copy setup header onto bootparams. Documentation/x86/boot.txt */
  342. setup_header_size = 0x0202 + kernel[0x0201] - setup_hdr_offset;
  343. /* Is there a limit on setup header size? */
  344. memcpy(&params->hdr, (kernel + setup_hdr_offset), setup_header_size);
  345. ret = kexec_add_buffer(image, (char *)params, params_misc_sz,
  346. params_misc_sz, 16, MIN_BOOTPARAM_ADDR,
  347. ULONG_MAX, 1, &bootparam_load_addr);
  348. if (ret)
  349. goto out_free_params;
  350. pr_debug("Loaded boot_param, command line and misc at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
  351. bootparam_load_addr, params_misc_sz, params_misc_sz);
  352. /* Load kernel */
  353. kernel_buf = kernel + kern16_size;
  354. kernel_bufsz = kernel_len - kern16_size;
  355. kernel_memsz = PAGE_ALIGN(header->init_size);
  356. kernel_align = header->kernel_alignment;
  357. ret = kexec_add_buffer(image, kernel_buf,
  358. kernel_bufsz, kernel_memsz, kernel_align,
  359. MIN_KERNEL_LOAD_ADDR, ULONG_MAX, 1,
  360. &kernel_load_addr);
  361. if (ret)
  362. goto out_free_params;
  363. pr_debug("Loaded 64bit kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
  364. kernel_load_addr, kernel_memsz, kernel_memsz);
  365. /* Load initrd high */
  366. if (initrd) {
  367. ret = kexec_add_buffer(image, initrd, initrd_len, initrd_len,
  368. PAGE_SIZE, MIN_INITRD_LOAD_ADDR,
  369. ULONG_MAX, 1, &initrd_load_addr);
  370. if (ret)
  371. goto out_free_params;
  372. pr_debug("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
  373. initrd_load_addr, initrd_len, initrd_len);
  374. setup_initrd(params, initrd_load_addr, initrd_len);
  375. }
  376. setup_cmdline(image, params, bootparam_load_addr,
  377. sizeof(struct boot_params), cmdline, cmdline_len);
  378. /* bootloader info. Do we need a separate ID for kexec kernel loader? */
  379. params->hdr.type_of_loader = 0x0D << 4;
  380. params->hdr.loadflags = 0;
  381. /* Setup purgatory regs for entry */
  382. ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", &regs64,
  383. sizeof(regs64), 1);
  384. if (ret)
  385. goto out_free_params;
  386. regs64.rbx = 0; /* Bootstrap Processor */
  387. regs64.rsi = bootparam_load_addr;
  388. regs64.rip = kernel_load_addr + 0x200;
  389. stack = kexec_purgatory_get_symbol_addr(image, "stack_end");
  390. if (IS_ERR(stack)) {
  391. pr_err("Could not find address of symbol stack_end\n");
  392. ret = -EINVAL;
  393. goto out_free_params;
  394. }
  395. regs64.rsp = (unsigned long)stack;
  396. ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", &regs64,
  397. sizeof(regs64), 0);
  398. if (ret)
  399. goto out_free_params;
  400. ret = setup_boot_parameters(image, params, bootparam_load_addr,
  401. efi_map_offset, efi_map_sz,
  402. efi_setup_data_offset);
  403. if (ret)
  404. goto out_free_params;
  405. /* Allocate loader specific data */
  406. ldata = kzalloc(sizeof(struct bzimage64_data), GFP_KERNEL);
  407. if (!ldata) {
  408. ret = -ENOMEM;
  409. goto out_free_params;
  410. }
  411. /*
  412. * Store pointer to params so that it could be freed after loading
  413. * params segment has been loaded and contents have been copied
  414. * somewhere else.
  415. */
  416. ldata->bootparams_buf = params;
  417. return ldata;
  418. out_free_params:
  419. kfree(params);
  420. return ERR_PTR(ret);
  421. }
  422. /* This cleanup function is called after various segments have been loaded */
  423. static int bzImage64_cleanup(void *loader_data)
  424. {
  425. struct bzimage64_data *ldata = loader_data;
  426. if (!ldata)
  427. return 0;
  428. kfree(ldata->bootparams_buf);
  429. ldata->bootparams_buf = NULL;
  430. return 0;
  431. }
  432. #ifdef CONFIG_KEXEC_BZIMAGE_VERIFY_SIG
  433. static int bzImage64_verify_sig(const char *kernel, unsigned long kernel_len)
  434. {
  435. return verify_pefile_signature(kernel, kernel_len,
  436. NULL,
  437. VERIFYING_KEXEC_PE_SIGNATURE);
  438. }
  439. #endif
  440. struct kexec_file_ops kexec_bzImage64_ops = {
  441. .probe = bzImage64_probe,
  442. .load = bzImage64_load,
  443. .cleanup = bzImage64_cleanup,
  444. #ifdef CONFIG_KEXEC_BZIMAGE_VERIFY_SIG
  445. .verify_sig = bzImage64_verify_sig,
  446. #endif
  447. };