kexec-bzimage64.c 15 KB

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