2
0

eif.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*
  2. * EIF (Enclave Image Format) related helpers
  3. *
  4. * Copyright (c) 2024 Dorjoy Chowdhury <dorjoychy111@gmail.com>
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or
  7. * (at your option) any later version. See the COPYING file in the
  8. * top-level directory.
  9. */
  10. #include "qemu/osdep.h"
  11. #include "qemu/bswap.h"
  12. #include "qapi/error.h"
  13. #include "crypto/hash.h"
  14. #include "crypto/x509-utils.h"
  15. #include <zlib.h> /* for crc32 */
  16. #include <cbor.h>
  17. #include "hw/core/eif.h"
  18. #define MAX_SECTIONS 32
  19. /* members are ordered according to field order in .eif file */
  20. typedef struct EifHeader {
  21. uint8_t magic[4]; /* must be .eif in ascii i.e., [46, 101, 105, 102] */
  22. uint16_t version;
  23. uint16_t flags;
  24. uint64_t default_memory;
  25. uint64_t default_cpus;
  26. uint16_t reserved;
  27. uint16_t section_cnt;
  28. uint64_t section_offsets[MAX_SECTIONS];
  29. uint64_t section_sizes[MAX_SECTIONS];
  30. uint32_t unused;
  31. uint32_t eif_crc32;
  32. } QEMU_PACKED EifHeader;
  33. /* members are ordered according to field order in .eif file */
  34. typedef struct EifSectionHeader {
  35. /*
  36. * 0 = invalid, 1 = kernel, 2 = cmdline, 3 = ramdisk, 4 = signature,
  37. * 5 = metadata
  38. */
  39. uint16_t section_type;
  40. uint16_t flags;
  41. uint64_t section_size;
  42. } QEMU_PACKED EifSectionHeader;
  43. enum EifSectionTypes {
  44. EIF_SECTION_INVALID = 0,
  45. EIF_SECTION_KERNEL = 1,
  46. EIF_SECTION_CMDLINE = 2,
  47. EIF_SECTION_RAMDISK = 3,
  48. EIF_SECTION_SIGNATURE = 4,
  49. EIF_SECTION_METADATA = 5,
  50. EIF_SECTION_MAX = 6,
  51. };
  52. static const char *section_type_to_string(uint16_t type)
  53. {
  54. const char *str;
  55. switch (type) {
  56. case EIF_SECTION_INVALID:
  57. str = "invalid";
  58. break;
  59. case EIF_SECTION_KERNEL:
  60. str = "kernel";
  61. break;
  62. case EIF_SECTION_CMDLINE:
  63. str = "cmdline";
  64. break;
  65. case EIF_SECTION_RAMDISK:
  66. str = "ramdisk";
  67. break;
  68. case EIF_SECTION_SIGNATURE:
  69. str = "signature";
  70. break;
  71. case EIF_SECTION_METADATA:
  72. str = "metadata";
  73. break;
  74. default:
  75. str = "unknown";
  76. break;
  77. }
  78. return str;
  79. }
  80. static bool read_eif_header(FILE *f, EifHeader *header, uint32_t *crc,
  81. Error **errp)
  82. {
  83. size_t got;
  84. size_t header_size = sizeof(*header);
  85. got = fread(header, 1, header_size, f);
  86. if (got != header_size) {
  87. error_setg(errp, "Failed to read EIF header");
  88. return false;
  89. }
  90. if (memcmp(header->magic, ".eif", 4) != 0) {
  91. error_setg(errp, "Invalid EIF image. Magic mismatch.");
  92. return false;
  93. }
  94. /* Exclude header->eif_crc32 field from CRC calculation */
  95. *crc = crc32(*crc, (uint8_t *)header, header_size - 4);
  96. header->version = be16_to_cpu(header->version);
  97. header->flags = be16_to_cpu(header->flags);
  98. header->default_memory = be64_to_cpu(header->default_memory);
  99. header->default_cpus = be64_to_cpu(header->default_cpus);
  100. header->reserved = be16_to_cpu(header->reserved);
  101. header->section_cnt = be16_to_cpu(header->section_cnt);
  102. for (int i = 0; i < MAX_SECTIONS; ++i) {
  103. header->section_offsets[i] = be64_to_cpu(header->section_offsets[i]);
  104. }
  105. for (int i = 0; i < MAX_SECTIONS; ++i) {
  106. header->section_sizes[i] = be64_to_cpu(header->section_sizes[i]);
  107. if (header->section_sizes[i] > SSIZE_MAX) {
  108. error_setg(errp, "Invalid EIF image. Section size out of bounds");
  109. return false;
  110. }
  111. }
  112. header->unused = be32_to_cpu(header->unused);
  113. header->eif_crc32 = be32_to_cpu(header->eif_crc32);
  114. return true;
  115. }
  116. static bool read_eif_section_header(FILE *f, EifSectionHeader *section_header,
  117. uint32_t *crc, Error **errp)
  118. {
  119. size_t got;
  120. size_t section_header_size = sizeof(*section_header);
  121. got = fread(section_header, 1, section_header_size, f);
  122. if (got != section_header_size) {
  123. error_setg(errp, "Failed to read EIF section header");
  124. return false;
  125. }
  126. *crc = crc32(*crc, (uint8_t *)section_header, section_header_size);
  127. section_header->section_type = be16_to_cpu(section_header->section_type);
  128. section_header->flags = be16_to_cpu(section_header->flags);
  129. section_header->section_size = be64_to_cpu(section_header->section_size);
  130. return true;
  131. }
  132. /*
  133. * Upon success, the caller is responsible for unlinking and freeing *tmp_path.
  134. */
  135. static bool get_tmp_file(const char *template, char **tmp_path, Error **errp)
  136. {
  137. int tmp_fd;
  138. *tmp_path = NULL;
  139. tmp_fd = g_file_open_tmp(template, tmp_path, NULL);
  140. if (tmp_fd < 0 || *tmp_path == NULL) {
  141. error_setg(errp, "Failed to create temporary file for template %s",
  142. template);
  143. return false;
  144. }
  145. close(tmp_fd);
  146. return true;
  147. }
  148. static void safe_fclose(FILE *f)
  149. {
  150. if (f) {
  151. fclose(f);
  152. }
  153. }
  154. static void safe_unlink(char *f)
  155. {
  156. if (f) {
  157. unlink(f);
  158. }
  159. }
  160. /*
  161. * Upon success, the caller is reponsible for unlinking and freeing *kernel_path
  162. */
  163. static bool read_eif_kernel(FILE *f, uint64_t size, char **kernel_path,
  164. QCryptoHash *hash0, QCryptoHash *hash1,
  165. uint32_t *crc, Error **errp)
  166. {
  167. size_t got;
  168. FILE *tmp_file = NULL;
  169. uint8_t *kernel = g_try_malloc(size);
  170. if (!kernel) {
  171. error_setg(errp, "Out of memory reading kernel section");
  172. goto cleanup;
  173. }
  174. *kernel_path = NULL;
  175. if (!get_tmp_file("eif-kernel-XXXXXX", kernel_path, errp)) {
  176. goto cleanup;
  177. }
  178. tmp_file = fopen(*kernel_path, "wb");
  179. if (tmp_file == NULL) {
  180. error_setg_errno(errp, errno, "Failed to open temporary file %s",
  181. *kernel_path);
  182. goto cleanup;
  183. }
  184. got = fread(kernel, 1, size, f);
  185. if ((uint64_t) got != size) {
  186. error_setg(errp, "Failed to read EIF kernel section data");
  187. goto cleanup;
  188. }
  189. got = fwrite(kernel, 1, size, tmp_file);
  190. if ((uint64_t) got != size) {
  191. error_setg(errp, "Failed to write EIF kernel section data to temporary"
  192. " file");
  193. goto cleanup;
  194. }
  195. *crc = crc32(*crc, kernel, size);
  196. if (qcrypto_hash_update(hash0, (char *)kernel, size, errp) != 0 ||
  197. qcrypto_hash_update(hash1, (char *)kernel, size, errp) != 0) {
  198. goto cleanup;
  199. }
  200. g_free(kernel);
  201. fclose(tmp_file);
  202. return true;
  203. cleanup:
  204. safe_fclose(tmp_file);
  205. safe_unlink(*kernel_path);
  206. g_free(*kernel_path);
  207. *kernel_path = NULL;
  208. g_free(kernel);
  209. return false;
  210. }
  211. static bool read_eif_cmdline(FILE *f, uint64_t size, char *cmdline,
  212. QCryptoHash *hash0, QCryptoHash *hash1,
  213. uint32_t *crc, Error **errp)
  214. {
  215. size_t got = fread(cmdline, 1, size, f);
  216. if ((uint64_t) got != size) {
  217. error_setg(errp, "Failed to read EIF cmdline section data");
  218. return false;
  219. }
  220. *crc = crc32(*crc, (uint8_t *)cmdline, size);
  221. if (qcrypto_hash_update(hash0, cmdline, size, errp) != 0 ||
  222. qcrypto_hash_update(hash1, cmdline, size, errp) != 0) {
  223. return false;
  224. }
  225. return true;
  226. }
  227. static bool read_eif_ramdisk(FILE *eif, FILE *initrd, uint64_t size,
  228. QCryptoHash *hash0, QCryptoHash *h, uint32_t *crc,
  229. Error **errp)
  230. {
  231. size_t got;
  232. bool ret = false;
  233. uint8_t *ramdisk = g_try_malloc(size);
  234. if (!ramdisk) {
  235. error_setg(errp, "Out of memory reading initrd section");
  236. goto cleanup;
  237. }
  238. got = fread(ramdisk, 1, size, eif);
  239. if ((uint64_t) got != size) {
  240. error_setg(errp, "Failed to read EIF ramdisk section data");
  241. goto cleanup;
  242. }
  243. got = fwrite(ramdisk, 1, size, initrd);
  244. if ((uint64_t) got != size) {
  245. error_setg(errp, "Failed to write EIF ramdisk data to temporary file");
  246. goto cleanup;
  247. }
  248. *crc = crc32(*crc, ramdisk, size);
  249. if (qcrypto_hash_update(hash0, (char *)ramdisk, size, errp) != 0 ||
  250. qcrypto_hash_update(h, (char *)ramdisk, size, errp) != 0) {
  251. goto cleanup;
  252. }
  253. ret = true;
  254. cleanup:
  255. g_free(ramdisk);
  256. return ret;
  257. }
  258. static bool get_signature_fingerprint_sha384(FILE *eif, uint64_t size,
  259. uint8_t *sha384,
  260. uint32_t *crc,
  261. Error **errp)
  262. {
  263. size_t got;
  264. g_autofree uint8_t *sig = NULL;
  265. g_autofree uint8_t *cert = NULL;
  266. cbor_item_t *item = NULL;
  267. cbor_item_t *pcr0 = NULL;
  268. size_t len;
  269. size_t hash_len = QCRYPTO_HASH_DIGEST_LEN_SHA384;
  270. struct cbor_pair *pair;
  271. struct cbor_load_result result;
  272. bool ret = false;
  273. sig = g_try_malloc(size);
  274. if (!sig) {
  275. error_setg(errp, "Out of memory reading signature section");
  276. goto cleanup;
  277. }
  278. got = fread(sig, 1, size, eif);
  279. if ((uint64_t) got != size) {
  280. error_setg(errp, "Failed to read EIF signature section data");
  281. goto cleanup;
  282. }
  283. *crc = crc32(*crc, sig, size);
  284. item = cbor_load(sig, size, &result);
  285. if (!item || result.error.code != CBOR_ERR_NONE) {
  286. error_setg(errp, "Failed to load signature section data as CBOR");
  287. goto cleanup;
  288. }
  289. if (!cbor_isa_array(item) || cbor_array_size(item) < 1) {
  290. error_setg(errp, "Invalid signature CBOR");
  291. goto cleanup;
  292. }
  293. pcr0 = cbor_array_get(item, 0);
  294. if (!pcr0) {
  295. error_setg(errp, "Failed to get PCR0 signature");
  296. goto cleanup;
  297. }
  298. if (!cbor_isa_map(pcr0) || cbor_map_size(pcr0) != 2) {
  299. error_setg(errp, "Invalid signature CBOR");
  300. goto cleanup;
  301. }
  302. pair = cbor_map_handle(pcr0);
  303. if (!cbor_isa_string(pair->key) || cbor_string_length(pair->key) != 19 ||
  304. memcmp(cbor_string_handle(pair->key), "signing_certificate", 19) != 0) {
  305. error_setg(errp, "Invalid signautre CBOR");
  306. goto cleanup;
  307. }
  308. if (!cbor_isa_array(pair->value)) {
  309. error_setg(errp, "Invalid signature CBOR");
  310. goto cleanup;
  311. }
  312. len = cbor_array_size(pair->value);
  313. if (len == 0) {
  314. error_setg(errp, "Invalid signature CBOR");
  315. goto cleanup;
  316. }
  317. cert = g_try_malloc(len);
  318. if (!cert) {
  319. error_setg(errp, "Out of memory reading signature section");
  320. goto cleanup;
  321. }
  322. for (int i = 0; i < len; ++i) {
  323. cbor_item_t *tmp = cbor_array_get(pair->value, i);
  324. if (!tmp) {
  325. error_setg(errp, "Invalid signature CBOR");
  326. goto cleanup;
  327. }
  328. if (!cbor_isa_uint(tmp) || cbor_int_get_width(tmp) != CBOR_INT_8) {
  329. cbor_decref(&tmp);
  330. error_setg(errp, "Invalid signature CBOR");
  331. goto cleanup;
  332. }
  333. cert[i] = cbor_get_uint8(tmp);
  334. cbor_decref(&tmp);
  335. }
  336. if (qcrypto_get_x509_cert_fingerprint(cert, len, QCRYPTO_HASH_ALGO_SHA384,
  337. sha384, &hash_len, errp)) {
  338. goto cleanup;
  339. }
  340. ret = true;
  341. cleanup:
  342. if (pcr0) {
  343. cbor_decref(&pcr0);
  344. }
  345. if (item) {
  346. cbor_decref(&item);
  347. }
  348. return ret;
  349. }
  350. /* Expects file to have offset 0 before this function is called */
  351. static long get_file_size(FILE *f, Error **errp)
  352. {
  353. long size;
  354. if (fseek(f, 0, SEEK_END) != 0) {
  355. error_setg_errno(errp, errno, "Failed to seek to the end of file");
  356. return -1;
  357. }
  358. size = ftell(f);
  359. if (size == -1) {
  360. error_setg_errno(errp, errno, "Failed to get offset");
  361. return -1;
  362. }
  363. if (fseek(f, 0, SEEK_SET) != 0) {
  364. error_setg_errno(errp, errno, "Failed to seek back to the start");
  365. return -1;
  366. }
  367. return size;
  368. }
  369. static bool get_SHA384_hash(QCryptoHash *h, uint8_t *hash, Error **errp)
  370. {
  371. size_t hash_len = QCRYPTO_HASH_DIGEST_LEN_SHA384;
  372. return qcrypto_hash_finalize_bytes(h, &hash, &hash_len, errp) == 0;
  373. }
  374. /*
  375. * Upon success, the caller is reponsible for unlinking and freeing
  376. * *kernel_path, *initrd_path and freeing *cmdline.
  377. */
  378. bool read_eif_file(const char *eif_path, const char *machine_initrd,
  379. char **kernel_path, char **initrd_path, char **cmdline,
  380. uint8_t *image_hash, uint8_t *bootstrap_hash,
  381. uint8_t *app_hash, uint8_t *fingerprint_hash,
  382. bool *signature_found, Error **errp)
  383. {
  384. FILE *f = NULL;
  385. FILE *machine_initrd_f = NULL;
  386. FILE *initrd_path_f = NULL;
  387. long machine_initrd_size;
  388. uint32_t crc = 0;
  389. EifHeader eif_header;
  390. bool seen_sections[EIF_SECTION_MAX] = {false};
  391. /* kernel + ramdisks + cmdline SHA384 hash */
  392. g_autoptr(QCryptoHash) hash0 = NULL;
  393. /* kernel + boot ramdisk + cmdline SHA384 hash */
  394. g_autoptr(QCryptoHash) hash1 = NULL;
  395. /* application ramdisk(s) SHA384 hash */
  396. g_autoptr(QCryptoHash) hash2 = NULL;
  397. *signature_found = false;
  398. *kernel_path = *initrd_path = *cmdline = NULL;
  399. hash0 = qcrypto_hash_new(QCRYPTO_HASH_ALGO_SHA384, errp);
  400. if (!hash0) {
  401. goto cleanup;
  402. }
  403. hash1 = qcrypto_hash_new(QCRYPTO_HASH_ALGO_SHA384, errp);
  404. if (!hash1) {
  405. goto cleanup;
  406. }
  407. hash2 = qcrypto_hash_new(QCRYPTO_HASH_ALGO_SHA384, errp);
  408. if (!hash2) {
  409. goto cleanup;
  410. }
  411. f = fopen(eif_path, "rb");
  412. if (f == NULL) {
  413. error_setg_errno(errp, errno, "Failed to open %s", eif_path);
  414. goto cleanup;
  415. }
  416. if (!read_eif_header(f, &eif_header, &crc, errp)) {
  417. goto cleanup;
  418. }
  419. if (eif_header.version < 4) {
  420. error_setg(errp, "Expected EIF version 4 or greater");
  421. goto cleanup;
  422. }
  423. if (eif_header.flags != 0) {
  424. error_setg(errp, "Expected EIF flags to be 0");
  425. goto cleanup;
  426. }
  427. if (eif_header.section_cnt > MAX_SECTIONS) {
  428. error_setg(errp, "EIF header section count must not be greater than "
  429. "%d but found %d", MAX_SECTIONS, eif_header.section_cnt);
  430. goto cleanup;
  431. }
  432. for (int i = 0; i < eif_header.section_cnt; ++i) {
  433. EifSectionHeader hdr;
  434. uint16_t section_type;
  435. if (eif_header.section_offsets[i] > OFF_MAX) {
  436. error_setg(errp, "Invalid EIF image. Section offset out of bounds");
  437. goto cleanup;
  438. }
  439. if (fseek(f, eif_header.section_offsets[i], SEEK_SET) != 0) {
  440. error_setg_errno(errp, errno, "Failed to offset to %" PRIu64 " in EIF file",
  441. eif_header.section_offsets[i]);
  442. goto cleanup;
  443. }
  444. if (!read_eif_section_header(f, &hdr, &crc, errp)) {
  445. goto cleanup;
  446. }
  447. if (hdr.flags != 0) {
  448. error_setg(errp, "Expected EIF section header flags to be 0");
  449. goto cleanup;
  450. }
  451. if (eif_header.section_sizes[i] != hdr.section_size) {
  452. error_setg(errp, "EIF section size mismatch between header and "
  453. "section header: header %" PRIu64 ", section header %" PRIu64,
  454. eif_header.section_sizes[i],
  455. hdr.section_size);
  456. goto cleanup;
  457. }
  458. section_type = hdr.section_type;
  459. switch (section_type) {
  460. case EIF_SECTION_KERNEL:
  461. if (seen_sections[EIF_SECTION_KERNEL]) {
  462. error_setg(errp, "Invalid EIF image. More than 1 kernel "
  463. "section");
  464. goto cleanup;
  465. }
  466. if (!read_eif_kernel(f, hdr.section_size, kernel_path, hash0,
  467. hash1, &crc, errp)) {
  468. goto cleanup;
  469. }
  470. break;
  471. case EIF_SECTION_CMDLINE:
  472. {
  473. uint64_t size;
  474. if (seen_sections[EIF_SECTION_CMDLINE]) {
  475. error_setg(errp, "Invalid EIF image. More than 1 cmdline "
  476. "section");
  477. goto cleanup;
  478. }
  479. size = hdr.section_size;
  480. *cmdline = g_try_malloc(size + 1);
  481. if (!*cmdline) {
  482. error_setg(errp, "Out of memory reading command line section");
  483. goto cleanup;
  484. }
  485. if (!read_eif_cmdline(f, size, *cmdline, hash0, hash1, &crc,
  486. errp)) {
  487. goto cleanup;
  488. }
  489. (*cmdline)[size] = '\0';
  490. break;
  491. }
  492. case EIF_SECTION_RAMDISK:
  493. {
  494. QCryptoHash *h = hash2;
  495. if (!seen_sections[EIF_SECTION_RAMDISK]) {
  496. /*
  497. * If this is the first time we are seeing a ramdisk section,
  498. * we need to:
  499. * 1) hash it into bootstrap (hash1) instead of app (hash2)
  500. * along with image (hash0)
  501. * 2) create the initrd temporary file.
  502. */
  503. h = hash1;
  504. if (!get_tmp_file("eif-initrd-XXXXXX", initrd_path, errp)) {
  505. goto cleanup;
  506. }
  507. initrd_path_f = fopen(*initrd_path, "wb");
  508. if (initrd_path_f == NULL) {
  509. error_setg_errno(errp, errno, "Failed to open file %s",
  510. *initrd_path);
  511. goto cleanup;
  512. }
  513. }
  514. if (!read_eif_ramdisk(f, initrd_path_f, hdr.section_size, hash0, h,
  515. &crc, errp)) {
  516. goto cleanup;
  517. }
  518. break;
  519. }
  520. case EIF_SECTION_SIGNATURE:
  521. *signature_found = true;
  522. if (!get_signature_fingerprint_sha384(f, hdr.section_size,
  523. fingerprint_hash, &crc,
  524. errp)) {
  525. goto cleanup;
  526. }
  527. break;
  528. default:
  529. /* other sections including invalid or unknown sections */
  530. {
  531. uint8_t *buf;
  532. size_t got;
  533. uint64_t size = hdr.section_size;
  534. buf = g_try_malloc(size);
  535. if (!buf) {
  536. error_setg(errp, "Out of memory reading unknown section");
  537. goto cleanup;
  538. }
  539. got = fread(buf, 1, size, f);
  540. if ((uint64_t) got != size) {
  541. g_free(buf);
  542. error_setg(errp, "Failed to read EIF %s section data",
  543. section_type_to_string(section_type));
  544. goto cleanup;
  545. }
  546. crc = crc32(crc, buf, size);
  547. g_free(buf);
  548. break;
  549. }
  550. }
  551. if (section_type < EIF_SECTION_MAX) {
  552. seen_sections[section_type] = true;
  553. }
  554. }
  555. if (!seen_sections[EIF_SECTION_KERNEL]) {
  556. error_setg(errp, "Invalid EIF image. No kernel section.");
  557. goto cleanup;
  558. }
  559. if (!seen_sections[EIF_SECTION_CMDLINE]) {
  560. error_setg(errp, "Invalid EIF image. No cmdline section.");
  561. goto cleanup;
  562. }
  563. if (!seen_sections[EIF_SECTION_RAMDISK]) {
  564. error_setg(errp, "Invalid EIF image. No ramdisk section.");
  565. goto cleanup;
  566. }
  567. if (eif_header.eif_crc32 != crc) {
  568. error_setg(errp, "CRC mismatch. Expected %u but header has %u.",
  569. crc, eif_header.eif_crc32);
  570. goto cleanup;
  571. }
  572. /*
  573. * Let's append the initrd file from "-initrd" option if any. Although
  574. * we pass the crc pointer to read_eif_ramdisk, it is not useful anymore.
  575. * We have already done the crc mismatch check above this code.
  576. */
  577. if (machine_initrd) {
  578. machine_initrd_f = fopen(machine_initrd, "rb");
  579. if (machine_initrd_f == NULL) {
  580. error_setg_errno(errp, errno, "Failed to open initrd file %s",
  581. machine_initrd);
  582. goto cleanup;
  583. }
  584. machine_initrd_size = get_file_size(machine_initrd_f, errp);
  585. if (machine_initrd_size == -1) {
  586. goto cleanup;
  587. }
  588. if (!read_eif_ramdisk(machine_initrd_f, initrd_path_f,
  589. machine_initrd_size, hash0, hash2, &crc, errp)) {
  590. goto cleanup;
  591. }
  592. }
  593. if (!get_SHA384_hash(hash0, image_hash, errp)) {
  594. goto cleanup;
  595. }
  596. if (!get_SHA384_hash(hash1, bootstrap_hash, errp)) {
  597. goto cleanup;
  598. }
  599. if (!get_SHA384_hash(hash2, app_hash, errp)) {
  600. goto cleanup;
  601. }
  602. fclose(f);
  603. fclose(initrd_path_f);
  604. safe_fclose(machine_initrd_f);
  605. return true;
  606. cleanup:
  607. safe_fclose(f);
  608. safe_fclose(initrd_path_f);
  609. safe_fclose(machine_initrd_f);
  610. safe_unlink(*kernel_path);
  611. g_free(*kernel_path);
  612. *kernel_path = NULL;
  613. safe_unlink(*initrd_path);
  614. g_free(*initrd_path);
  615. *initrd_path = NULL;
  616. g_free(*cmdline);
  617. *cmdline = NULL;
  618. return false;
  619. }