kexec_elf_64.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /*
  2. * Load ELF vmlinux file for the kexec_file_load syscall.
  3. *
  4. * Copyright (C) 2004 Adam Litke (agl@us.ibm.com)
  5. * Copyright (C) 2004 IBM Corp.
  6. * Copyright (C) 2005 R Sharada (sharada@in.ibm.com)
  7. * Copyright (C) 2006 Mohan Kumar M (mohan@in.ibm.com)
  8. * Copyright (C) 2016 IBM Corporation
  9. *
  10. * Based on kexec-tools' kexec-elf-exec.c and kexec-elf-ppc64.c.
  11. * Heavily modified for the kernel by
  12. * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation (version 2 of the License).
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. */
  23. #define pr_fmt(fmt) "kexec_elf: " fmt
  24. #include <linux/elf.h>
  25. #include <linux/kexec.h>
  26. #include <linux/libfdt.h>
  27. #include <linux/module.h>
  28. #include <linux/of_fdt.h>
  29. #include <linux/slab.h>
  30. #include <linux/types.h>
  31. #define PURGATORY_STACK_SIZE (16 * 1024)
  32. #define elf_addr_to_cpu elf64_to_cpu
  33. #ifndef Elf_Rel
  34. #define Elf_Rel Elf64_Rel
  35. #endif /* Elf_Rel */
  36. struct elf_info {
  37. /*
  38. * Where the ELF binary contents are kept.
  39. * Memory managed by the user of the struct.
  40. */
  41. const char *buffer;
  42. const struct elfhdr *ehdr;
  43. const struct elf_phdr *proghdrs;
  44. struct elf_shdr *sechdrs;
  45. };
  46. static inline bool elf_is_elf_file(const struct elfhdr *ehdr)
  47. {
  48. return memcmp(ehdr->e_ident, ELFMAG, SELFMAG) == 0;
  49. }
  50. static uint64_t elf64_to_cpu(const struct elfhdr *ehdr, uint64_t value)
  51. {
  52. if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB)
  53. value = le64_to_cpu(value);
  54. else if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
  55. value = be64_to_cpu(value);
  56. return value;
  57. }
  58. static uint16_t elf16_to_cpu(const struct elfhdr *ehdr, uint16_t value)
  59. {
  60. if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB)
  61. value = le16_to_cpu(value);
  62. else if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
  63. value = be16_to_cpu(value);
  64. return value;
  65. }
  66. static uint32_t elf32_to_cpu(const struct elfhdr *ehdr, uint32_t value)
  67. {
  68. if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB)
  69. value = le32_to_cpu(value);
  70. else if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
  71. value = be32_to_cpu(value);
  72. return value;
  73. }
  74. /**
  75. * elf_is_ehdr_sane - check that it is safe to use the ELF header
  76. * @buf_len: size of the buffer in which the ELF file is loaded.
  77. */
  78. static bool elf_is_ehdr_sane(const struct elfhdr *ehdr, size_t buf_len)
  79. {
  80. if (ehdr->e_phnum > 0 && ehdr->e_phentsize != sizeof(struct elf_phdr)) {
  81. pr_debug("Bad program header size.\n");
  82. return false;
  83. } else if (ehdr->e_shnum > 0 &&
  84. ehdr->e_shentsize != sizeof(struct elf_shdr)) {
  85. pr_debug("Bad section header size.\n");
  86. return false;
  87. } else if (ehdr->e_ident[EI_VERSION] != EV_CURRENT ||
  88. ehdr->e_version != EV_CURRENT) {
  89. pr_debug("Unknown ELF version.\n");
  90. return false;
  91. }
  92. if (ehdr->e_phoff > 0 && ehdr->e_phnum > 0) {
  93. size_t phdr_size;
  94. /*
  95. * e_phnum is at most 65535 so calculating the size of the
  96. * program header cannot overflow.
  97. */
  98. phdr_size = sizeof(struct elf_phdr) * ehdr->e_phnum;
  99. /* Sanity check the program header table location. */
  100. if (ehdr->e_phoff + phdr_size < ehdr->e_phoff) {
  101. pr_debug("Program headers at invalid location.\n");
  102. return false;
  103. } else if (ehdr->e_phoff + phdr_size > buf_len) {
  104. pr_debug("Program headers truncated.\n");
  105. return false;
  106. }
  107. }
  108. if (ehdr->e_shoff > 0 && ehdr->e_shnum > 0) {
  109. size_t shdr_size;
  110. /*
  111. * e_shnum is at most 65536 so calculating
  112. * the size of the section header cannot overflow.
  113. */
  114. shdr_size = sizeof(struct elf_shdr) * ehdr->e_shnum;
  115. /* Sanity check the section header table location. */
  116. if (ehdr->e_shoff + shdr_size < ehdr->e_shoff) {
  117. pr_debug("Section headers at invalid location.\n");
  118. return false;
  119. } else if (ehdr->e_shoff + shdr_size > buf_len) {
  120. pr_debug("Section headers truncated.\n");
  121. return false;
  122. }
  123. }
  124. return true;
  125. }
  126. static int elf_read_ehdr(const char *buf, size_t len, struct elfhdr *ehdr)
  127. {
  128. struct elfhdr *buf_ehdr;
  129. if (len < sizeof(*buf_ehdr)) {
  130. pr_debug("Buffer is too small to hold ELF header.\n");
  131. return -ENOEXEC;
  132. }
  133. memset(ehdr, 0, sizeof(*ehdr));
  134. memcpy(ehdr->e_ident, buf, sizeof(ehdr->e_ident));
  135. if (!elf_is_elf_file(ehdr)) {
  136. pr_debug("No ELF header magic.\n");
  137. return -ENOEXEC;
  138. }
  139. if (ehdr->e_ident[EI_CLASS] != ELF_CLASS) {
  140. pr_debug("Not a supported ELF class.\n");
  141. return -ENOEXEC;
  142. } else if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB &&
  143. ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
  144. pr_debug("Not a supported ELF data format.\n");
  145. return -ENOEXEC;
  146. }
  147. buf_ehdr = (struct elfhdr *) buf;
  148. if (elf16_to_cpu(ehdr, buf_ehdr->e_ehsize) != sizeof(*buf_ehdr)) {
  149. pr_debug("Bad ELF header size.\n");
  150. return -ENOEXEC;
  151. }
  152. ehdr->e_type = elf16_to_cpu(ehdr, buf_ehdr->e_type);
  153. ehdr->e_machine = elf16_to_cpu(ehdr, buf_ehdr->e_machine);
  154. ehdr->e_version = elf32_to_cpu(ehdr, buf_ehdr->e_version);
  155. ehdr->e_entry = elf_addr_to_cpu(ehdr, buf_ehdr->e_entry);
  156. ehdr->e_phoff = elf_addr_to_cpu(ehdr, buf_ehdr->e_phoff);
  157. ehdr->e_shoff = elf_addr_to_cpu(ehdr, buf_ehdr->e_shoff);
  158. ehdr->e_flags = elf32_to_cpu(ehdr, buf_ehdr->e_flags);
  159. ehdr->e_phentsize = elf16_to_cpu(ehdr, buf_ehdr->e_phentsize);
  160. ehdr->e_phnum = elf16_to_cpu(ehdr, buf_ehdr->e_phnum);
  161. ehdr->e_shentsize = elf16_to_cpu(ehdr, buf_ehdr->e_shentsize);
  162. ehdr->e_shnum = elf16_to_cpu(ehdr, buf_ehdr->e_shnum);
  163. ehdr->e_shstrndx = elf16_to_cpu(ehdr, buf_ehdr->e_shstrndx);
  164. return elf_is_ehdr_sane(ehdr, len) ? 0 : -ENOEXEC;
  165. }
  166. /**
  167. * elf_is_phdr_sane - check that it is safe to use the program header
  168. * @buf_len: size of the buffer in which the ELF file is loaded.
  169. */
  170. static bool elf_is_phdr_sane(const struct elf_phdr *phdr, size_t buf_len)
  171. {
  172. if (phdr->p_offset + phdr->p_filesz < phdr->p_offset) {
  173. pr_debug("ELF segment location wraps around.\n");
  174. return false;
  175. } else if (phdr->p_offset + phdr->p_filesz > buf_len) {
  176. pr_debug("ELF segment not in file.\n");
  177. return false;
  178. } else if (phdr->p_paddr + phdr->p_memsz < phdr->p_paddr) {
  179. pr_debug("ELF segment address wraps around.\n");
  180. return false;
  181. }
  182. return true;
  183. }
  184. static int elf_read_phdr(const char *buf, size_t len, struct elf_info *elf_info,
  185. int idx)
  186. {
  187. /* Override the const in proghdrs, we are the ones doing the loading. */
  188. struct elf_phdr *phdr = (struct elf_phdr *) &elf_info->proghdrs[idx];
  189. const char *pbuf;
  190. struct elf_phdr *buf_phdr;
  191. pbuf = buf + elf_info->ehdr->e_phoff + (idx * sizeof(*buf_phdr));
  192. buf_phdr = (struct elf_phdr *) pbuf;
  193. phdr->p_type = elf32_to_cpu(elf_info->ehdr, buf_phdr->p_type);
  194. phdr->p_offset = elf_addr_to_cpu(elf_info->ehdr, buf_phdr->p_offset);
  195. phdr->p_paddr = elf_addr_to_cpu(elf_info->ehdr, buf_phdr->p_paddr);
  196. phdr->p_vaddr = elf_addr_to_cpu(elf_info->ehdr, buf_phdr->p_vaddr);
  197. phdr->p_flags = elf32_to_cpu(elf_info->ehdr, buf_phdr->p_flags);
  198. /*
  199. * The following fields have a type equivalent to Elf_Addr
  200. * both in 32 bit and 64 bit ELF.
  201. */
  202. phdr->p_filesz = elf_addr_to_cpu(elf_info->ehdr, buf_phdr->p_filesz);
  203. phdr->p_memsz = elf_addr_to_cpu(elf_info->ehdr, buf_phdr->p_memsz);
  204. phdr->p_align = elf_addr_to_cpu(elf_info->ehdr, buf_phdr->p_align);
  205. return elf_is_phdr_sane(phdr, len) ? 0 : -ENOEXEC;
  206. }
  207. /**
  208. * elf_read_phdrs - read the program headers from the buffer
  209. *
  210. * This function assumes that the program header table was checked for sanity.
  211. * Use elf_is_ehdr_sane() if it wasn't.
  212. */
  213. static int elf_read_phdrs(const char *buf, size_t len,
  214. struct elf_info *elf_info)
  215. {
  216. size_t phdr_size, i;
  217. const struct elfhdr *ehdr = elf_info->ehdr;
  218. /*
  219. * e_phnum is at most 65535 so calculating the size of the
  220. * program header cannot overflow.
  221. */
  222. phdr_size = sizeof(struct elf_phdr) * ehdr->e_phnum;
  223. elf_info->proghdrs = kzalloc(phdr_size, GFP_KERNEL);
  224. if (!elf_info->proghdrs)
  225. return -ENOMEM;
  226. for (i = 0; i < ehdr->e_phnum; i++) {
  227. int ret;
  228. ret = elf_read_phdr(buf, len, elf_info, i);
  229. if (ret) {
  230. kfree(elf_info->proghdrs);
  231. elf_info->proghdrs = NULL;
  232. return ret;
  233. }
  234. }
  235. return 0;
  236. }
  237. /**
  238. * elf_is_shdr_sane - check that it is safe to use the section header
  239. * @buf_len: size of the buffer in which the ELF file is loaded.
  240. */
  241. static bool elf_is_shdr_sane(const struct elf_shdr *shdr, size_t buf_len)
  242. {
  243. bool size_ok;
  244. /* SHT_NULL headers have undefined values, so we can't check them. */
  245. if (shdr->sh_type == SHT_NULL)
  246. return true;
  247. /* Now verify sh_entsize */
  248. switch (shdr->sh_type) {
  249. case SHT_SYMTAB:
  250. size_ok = shdr->sh_entsize == sizeof(Elf_Sym);
  251. break;
  252. case SHT_RELA:
  253. size_ok = shdr->sh_entsize == sizeof(Elf_Rela);
  254. break;
  255. case SHT_DYNAMIC:
  256. size_ok = shdr->sh_entsize == sizeof(Elf_Dyn);
  257. break;
  258. case SHT_REL:
  259. size_ok = shdr->sh_entsize == sizeof(Elf_Rel);
  260. break;
  261. case SHT_NOTE:
  262. case SHT_PROGBITS:
  263. case SHT_HASH:
  264. case SHT_NOBITS:
  265. default:
  266. /*
  267. * This is a section whose entsize requirements
  268. * I don't care about. If I don't know about
  269. * the section I can't care about it's entsize
  270. * requirements.
  271. */
  272. size_ok = true;
  273. break;
  274. }
  275. if (!size_ok) {
  276. pr_debug("ELF section with wrong entry size.\n");
  277. return false;
  278. } else if (shdr->sh_addr + shdr->sh_size < shdr->sh_addr) {
  279. pr_debug("ELF section address wraps around.\n");
  280. return false;
  281. }
  282. if (shdr->sh_type != SHT_NOBITS) {
  283. if (shdr->sh_offset + shdr->sh_size < shdr->sh_offset) {
  284. pr_debug("ELF section location wraps around.\n");
  285. return false;
  286. } else if (shdr->sh_offset + shdr->sh_size > buf_len) {
  287. pr_debug("ELF section not in file.\n");
  288. return false;
  289. }
  290. }
  291. return true;
  292. }
  293. static int elf_read_shdr(const char *buf, size_t len, struct elf_info *elf_info,
  294. int idx)
  295. {
  296. struct elf_shdr *shdr = &elf_info->sechdrs[idx];
  297. const struct elfhdr *ehdr = elf_info->ehdr;
  298. const char *sbuf;
  299. struct elf_shdr *buf_shdr;
  300. sbuf = buf + ehdr->e_shoff + idx * sizeof(*buf_shdr);
  301. buf_shdr = (struct elf_shdr *) sbuf;
  302. shdr->sh_name = elf32_to_cpu(ehdr, buf_shdr->sh_name);
  303. shdr->sh_type = elf32_to_cpu(ehdr, buf_shdr->sh_type);
  304. shdr->sh_addr = elf_addr_to_cpu(ehdr, buf_shdr->sh_addr);
  305. shdr->sh_offset = elf_addr_to_cpu(ehdr, buf_shdr->sh_offset);
  306. shdr->sh_link = elf32_to_cpu(ehdr, buf_shdr->sh_link);
  307. shdr->sh_info = elf32_to_cpu(ehdr, buf_shdr->sh_info);
  308. /*
  309. * The following fields have a type equivalent to Elf_Addr
  310. * both in 32 bit and 64 bit ELF.
  311. */
  312. shdr->sh_flags = elf_addr_to_cpu(ehdr, buf_shdr->sh_flags);
  313. shdr->sh_size = elf_addr_to_cpu(ehdr, buf_shdr->sh_size);
  314. shdr->sh_addralign = elf_addr_to_cpu(ehdr, buf_shdr->sh_addralign);
  315. shdr->sh_entsize = elf_addr_to_cpu(ehdr, buf_shdr->sh_entsize);
  316. return elf_is_shdr_sane(shdr, len) ? 0 : -ENOEXEC;
  317. }
  318. /**
  319. * elf_read_shdrs - read the section headers from the buffer
  320. *
  321. * This function assumes that the section header table was checked for sanity.
  322. * Use elf_is_ehdr_sane() if it wasn't.
  323. */
  324. static int elf_read_shdrs(const char *buf, size_t len,
  325. struct elf_info *elf_info)
  326. {
  327. size_t shdr_size, i;
  328. /*
  329. * e_shnum is at most 65536 so calculating
  330. * the size of the section header cannot overflow.
  331. */
  332. shdr_size = sizeof(struct elf_shdr) * elf_info->ehdr->e_shnum;
  333. elf_info->sechdrs = kzalloc(shdr_size, GFP_KERNEL);
  334. if (!elf_info->sechdrs)
  335. return -ENOMEM;
  336. for (i = 0; i < elf_info->ehdr->e_shnum; i++) {
  337. int ret;
  338. ret = elf_read_shdr(buf, len, elf_info, i);
  339. if (ret) {
  340. kfree(elf_info->sechdrs);
  341. elf_info->sechdrs = NULL;
  342. return ret;
  343. }
  344. }
  345. return 0;
  346. }
  347. /**
  348. * elf_read_from_buffer - read ELF file and sets up ELF header and ELF info
  349. * @buf: Buffer to read ELF file from.
  350. * @len: Size of @buf.
  351. * @ehdr: Pointer to existing struct which will be populated.
  352. * @elf_info: Pointer to existing struct which will be populated.
  353. *
  354. * This function allows reading ELF files with different byte order than
  355. * the kernel, byte-swapping the fields as needed.
  356. *
  357. * Return:
  358. * On success returns 0, and the caller should call elf_free_info(elf_info) to
  359. * free the memory allocated for the section and program headers.
  360. */
  361. int elf_read_from_buffer(const char *buf, size_t len, struct elfhdr *ehdr,
  362. struct elf_info *elf_info)
  363. {
  364. int ret;
  365. ret = elf_read_ehdr(buf, len, ehdr);
  366. if (ret)
  367. return ret;
  368. elf_info->buffer = buf;
  369. elf_info->ehdr = ehdr;
  370. if (ehdr->e_phoff > 0 && ehdr->e_phnum > 0) {
  371. ret = elf_read_phdrs(buf, len, elf_info);
  372. if (ret)
  373. return ret;
  374. }
  375. if (ehdr->e_shoff > 0 && ehdr->e_shnum > 0) {
  376. ret = elf_read_shdrs(buf, len, elf_info);
  377. if (ret) {
  378. kfree(elf_info->proghdrs);
  379. return ret;
  380. }
  381. }
  382. return 0;
  383. }
  384. /**
  385. * elf_free_info - free memory allocated by elf_read_from_buffer
  386. */
  387. void elf_free_info(struct elf_info *elf_info)
  388. {
  389. kfree(elf_info->proghdrs);
  390. kfree(elf_info->sechdrs);
  391. memset(elf_info, 0, sizeof(*elf_info));
  392. }
  393. /**
  394. * build_elf_exec_info - read ELF executable and check that we can use it
  395. */
  396. static int build_elf_exec_info(const char *buf, size_t len, struct elfhdr *ehdr,
  397. struct elf_info *elf_info)
  398. {
  399. int i;
  400. int ret;
  401. ret = elf_read_from_buffer(buf, len, ehdr, elf_info);
  402. if (ret)
  403. return ret;
  404. /* Big endian vmlinux has type ET_DYN. */
  405. if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
  406. pr_err("Not an ELF executable.\n");
  407. goto error;
  408. } else if (!elf_info->proghdrs) {
  409. pr_err("No ELF program header.\n");
  410. goto error;
  411. }
  412. for (i = 0; i < ehdr->e_phnum; i++) {
  413. /*
  414. * Kexec does not support loading interpreters.
  415. * In addition this check keeps us from attempting
  416. * to kexec ordinay executables.
  417. */
  418. if (elf_info->proghdrs[i].p_type == PT_INTERP) {
  419. pr_err("Requires an ELF interpreter.\n");
  420. goto error;
  421. }
  422. }
  423. return 0;
  424. error:
  425. elf_free_info(elf_info);
  426. return -ENOEXEC;
  427. }
  428. static int elf64_probe(const char *buf, unsigned long len)
  429. {
  430. struct elfhdr ehdr;
  431. struct elf_info elf_info;
  432. int ret;
  433. ret = build_elf_exec_info(buf, len, &ehdr, &elf_info);
  434. if (ret)
  435. return ret;
  436. elf_free_info(&elf_info);
  437. return elf_check_arch(&ehdr) ? 0 : -ENOEXEC;
  438. }
  439. /**
  440. * elf_exec_load - load ELF executable image
  441. * @lowest_load_addr: On return, will be the address where the first PT_LOAD
  442. * section will be loaded in memory.
  443. *
  444. * Return:
  445. * 0 on success, negative value on failure.
  446. */
  447. static int elf_exec_load(struct kimage *image, struct elfhdr *ehdr,
  448. struct elf_info *elf_info,
  449. unsigned long *lowest_load_addr)
  450. {
  451. unsigned long base = 0, lowest_addr = UINT_MAX;
  452. int ret;
  453. size_t i;
  454. struct kexec_buf kbuf = { .image = image, .buf_max = ppc64_rma_size,
  455. .top_down = false };
  456. /* Read in the PT_LOAD segments. */
  457. for (i = 0; i < ehdr->e_phnum; i++) {
  458. unsigned long load_addr;
  459. size_t size;
  460. const struct elf_phdr *phdr;
  461. phdr = &elf_info->proghdrs[i];
  462. if (phdr->p_type != PT_LOAD)
  463. continue;
  464. size = phdr->p_filesz;
  465. if (size > phdr->p_memsz)
  466. size = phdr->p_memsz;
  467. kbuf.buffer = (void *) elf_info->buffer + phdr->p_offset;
  468. kbuf.bufsz = size;
  469. kbuf.memsz = phdr->p_memsz;
  470. kbuf.buf_align = phdr->p_align;
  471. kbuf.buf_min = phdr->p_paddr + base;
  472. ret = kexec_add_buffer(&kbuf);
  473. if (ret)
  474. goto out;
  475. load_addr = kbuf.mem;
  476. if (load_addr < lowest_addr)
  477. lowest_addr = load_addr;
  478. }
  479. /* Update entry point to reflect new load address. */
  480. ehdr->e_entry += base;
  481. *lowest_load_addr = lowest_addr;
  482. ret = 0;
  483. out:
  484. return ret;
  485. }
  486. static void *elf64_load(struct kimage *image, char *kernel_buf,
  487. unsigned long kernel_len, char *initrd,
  488. unsigned long initrd_len, char *cmdline,
  489. unsigned long cmdline_len)
  490. {
  491. int ret;
  492. unsigned int fdt_size;
  493. unsigned long kernel_load_addr;
  494. unsigned long initrd_load_addr = 0, fdt_load_addr;
  495. void *fdt;
  496. const void *slave_code;
  497. struct elfhdr ehdr;
  498. struct elf_info elf_info;
  499. struct kexec_buf kbuf = { .image = image, .buf_min = 0,
  500. .buf_max = ppc64_rma_size };
  501. struct kexec_buf pbuf = { .image = image, .buf_min = 0,
  502. .buf_max = ppc64_rma_size, .top_down = true };
  503. ret = build_elf_exec_info(kernel_buf, kernel_len, &ehdr, &elf_info);
  504. if (ret)
  505. goto out;
  506. ret = elf_exec_load(image, &ehdr, &elf_info, &kernel_load_addr);
  507. if (ret)
  508. goto out;
  509. pr_debug("Loaded the kernel at 0x%lx\n", kernel_load_addr);
  510. ret = kexec_load_purgatory(image, &pbuf);
  511. if (ret) {
  512. pr_err("Loading purgatory failed.\n");
  513. goto out;
  514. }
  515. pr_debug("Loaded purgatory at 0x%lx\n", pbuf.mem);
  516. if (initrd != NULL) {
  517. kbuf.buffer = initrd;
  518. kbuf.bufsz = kbuf.memsz = initrd_len;
  519. kbuf.buf_align = PAGE_SIZE;
  520. kbuf.top_down = false;
  521. ret = kexec_add_buffer(&kbuf);
  522. if (ret)
  523. goto out;
  524. initrd_load_addr = kbuf.mem;
  525. pr_debug("Loaded initrd at 0x%lx\n", initrd_load_addr);
  526. }
  527. fdt_size = fdt_totalsize(initial_boot_params) * 2;
  528. fdt = kmalloc(fdt_size, GFP_KERNEL);
  529. if (!fdt) {
  530. pr_err("Not enough memory for the device tree.\n");
  531. ret = -ENOMEM;
  532. goto out;
  533. }
  534. ret = fdt_open_into(initial_boot_params, fdt, fdt_size);
  535. if (ret < 0) {
  536. pr_err("Error setting up the new device tree.\n");
  537. ret = -EINVAL;
  538. goto out;
  539. }
  540. ret = setup_new_fdt(image, fdt, initrd_load_addr, initrd_len, cmdline);
  541. if (ret)
  542. goto out;
  543. fdt_pack(fdt);
  544. kbuf.buffer = fdt;
  545. kbuf.bufsz = kbuf.memsz = fdt_size;
  546. kbuf.buf_align = PAGE_SIZE;
  547. kbuf.top_down = true;
  548. ret = kexec_add_buffer(&kbuf);
  549. if (ret)
  550. goto out;
  551. fdt_load_addr = kbuf.mem;
  552. pr_debug("Loaded device tree at 0x%lx\n", fdt_load_addr);
  553. slave_code = elf_info.buffer + elf_info.proghdrs[0].p_offset;
  554. ret = setup_purgatory(image, slave_code, fdt, kernel_load_addr,
  555. fdt_load_addr);
  556. if (ret)
  557. pr_err("Error setting up the purgatory.\n");
  558. out:
  559. elf_free_info(&elf_info);
  560. /* Make kimage_file_post_load_cleanup free the fdt buffer for us. */
  561. return ret ? ERR_PTR(ret) : fdt;
  562. }
  563. const struct kexec_file_ops kexec_elf64_ops = {
  564. .probe = elf64_probe,
  565. .load = elf64_load,
  566. };