image-pre-load.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2021 Philippe Reynes <philippe.reynes@softathome.com>
  4. */
  5. #include <common.h>
  6. #include <asm/global_data.h>
  7. DECLARE_GLOBAL_DATA_PTR;
  8. #include <image.h>
  9. #include <mapmem.h>
  10. #include <u-boot/sha256.h>
  11. /*
  12. * Offset of the image
  13. *
  14. * This value is used to skip the header before really launching the image
  15. */
  16. ulong image_load_offset;
  17. /*
  18. * This function gathers information about the signature check
  19. * that could be done before launching the image.
  20. *
  21. * return:
  22. * < 0 => an error has occurred
  23. * 0 => OK
  24. * 1 => no setup
  25. */
  26. static int image_pre_load_sig_setup(struct image_sig_info *info)
  27. {
  28. const void *algo_name, *padding_name, *key, *mandatory;
  29. const u32 *sig_size;
  30. int key_len;
  31. int node, ret = 0;
  32. char *sig_info_path = NULL;
  33. if (!info) {
  34. log_err("ERROR: info is NULL for image pre-load sig check\n");
  35. ret = -EINVAL;
  36. goto out;
  37. }
  38. memset(info, 0, sizeof(*info));
  39. sig_info_path = env_get("pre_load_sig_info_path");
  40. if (!sig_info_path)
  41. sig_info_path = IMAGE_PRE_LOAD_PATH;
  42. node = fdt_path_offset(gd_fdt_blob(), sig_info_path);
  43. if (node < 0) {
  44. log_info("INFO: no info for image pre-load sig check\n");
  45. ret = 1;
  46. goto out;
  47. }
  48. algo_name = fdt_getprop(gd_fdt_blob(), node,
  49. IMAGE_PRE_LOAD_PROP_ALGO_NAME, NULL);
  50. if (!algo_name) {
  51. printf("ERROR: no algo_name for image pre-load sig check\n");
  52. ret = -EINVAL;
  53. goto out;
  54. }
  55. padding_name = fdt_getprop(gd_fdt_blob(), node,
  56. IMAGE_PRE_LOAD_PROP_PADDING_NAME, NULL);
  57. if (!padding_name) {
  58. log_info("INFO: no padding_name provided, so using pkcs-1.5\n");
  59. padding_name = "pkcs-1.5";
  60. }
  61. sig_size = fdt_getprop(gd_fdt_blob(), node,
  62. IMAGE_PRE_LOAD_PROP_SIG_SIZE, NULL);
  63. if (!sig_size) {
  64. log_err("ERROR: no signature-size for image pre-load sig check\n");
  65. ret = -EINVAL;
  66. goto out;
  67. }
  68. key = fdt_getprop(gd_fdt_blob(), node,
  69. IMAGE_PRE_LOAD_PROP_PUBLIC_KEY, &key_len);
  70. if (!key) {
  71. log_err("ERROR: no key for image pre-load sig check\n");
  72. ret = -EINVAL;
  73. goto out;
  74. }
  75. info->algo_name = (char *)algo_name;
  76. info->padding_name = (char *)padding_name;
  77. info->key = (uint8_t *)key;
  78. info->key_len = key_len;
  79. info->sig_size = fdt32_to_cpu(*sig_size);
  80. mandatory = fdt_getprop(gd_fdt_blob(), node,
  81. IMAGE_PRE_LOAD_PROP_MANDATORY, NULL);
  82. if (mandatory && !strcmp((char *)mandatory, "yes"))
  83. info->mandatory = 1;
  84. /* Compute signature information */
  85. info->sig_info.name = info->algo_name;
  86. info->sig_info.padding = image_get_padding_algo(info->padding_name);
  87. info->sig_info.checksum = image_get_checksum_algo(info->sig_info.name);
  88. info->sig_info.crypto = image_get_crypto_algo(info->sig_info.name);
  89. info->sig_info.key = info->key;
  90. info->sig_info.keylen = info->key_len;
  91. out:
  92. return ret;
  93. }
  94. static int image_pre_load_sig_get_magic(ulong addr, u32 *magic)
  95. {
  96. struct sig_header_s *sig_header;
  97. int ret = 0;
  98. sig_header = (struct sig_header_s *)map_sysmem(addr, SIG_HEADER_LEN);
  99. if (!sig_header) {
  100. log_err("ERROR: can't map first header\n");
  101. ret = -EFAULT;
  102. goto out;
  103. }
  104. *magic = fdt32_to_cpu(sig_header->magic);
  105. unmap_sysmem(sig_header);
  106. out:
  107. return ret;
  108. }
  109. static int image_pre_load_sig_get_header_size(ulong addr, u32 *header_size)
  110. {
  111. struct sig_header_s *sig_header;
  112. int ret = 0;
  113. sig_header = (struct sig_header_s *)map_sysmem(addr, SIG_HEADER_LEN);
  114. if (!sig_header) {
  115. log_err("ERROR: can't map first header\n");
  116. ret = -EFAULT;
  117. goto out;
  118. }
  119. *header_size = fdt32_to_cpu(sig_header->header_size);
  120. unmap_sysmem(sig_header);
  121. out:
  122. return ret;
  123. }
  124. /*
  125. * return:
  126. * < 0 => no magic and magic mandatory (or error when reading magic)
  127. * 0 => magic found
  128. * 1 => magic NOT found
  129. */
  130. static int image_pre_load_sig_check_magic(struct image_sig_info *info, ulong addr)
  131. {
  132. u32 magic;
  133. int ret = 1;
  134. ret = image_pre_load_sig_get_magic(addr, &magic);
  135. if (ret < 0)
  136. goto out;
  137. if (magic != IMAGE_PRE_LOAD_SIG_MAGIC) {
  138. if (info->mandatory) {
  139. log_err("ERROR: signature is mandatory\n");
  140. ret = -EINVAL;
  141. goto out;
  142. }
  143. ret = 1;
  144. goto out;
  145. }
  146. ret = 0; /* magic found */
  147. out:
  148. return ret;
  149. }
  150. static int image_pre_load_sig_check_header_sig(struct image_sig_info *info, ulong addr)
  151. {
  152. void *header;
  153. struct image_region reg;
  154. u32 sig_len;
  155. u8 *sig;
  156. int ret = 0;
  157. /* Only map header of the header and its signature */
  158. header = (void *)map_sysmem(addr, SIG_HEADER_LEN + info->sig_size);
  159. if (!header) {
  160. log_err("ERROR: can't map header\n");
  161. ret = -EFAULT;
  162. goto out;
  163. }
  164. reg.data = header;
  165. reg.size = SIG_HEADER_LEN;
  166. sig = (uint8_t *)header + SIG_HEADER_LEN;
  167. sig_len = info->sig_size;
  168. ret = info->sig_info.crypto->verify(&info->sig_info, &reg, 1, sig, sig_len);
  169. if (ret) {
  170. log_err("ERROR: header signature check has failed (err=%d)\n", ret);
  171. ret = -EINVAL;
  172. goto out_unmap;
  173. }
  174. out_unmap:
  175. unmap_sysmem(header);
  176. out:
  177. return ret;
  178. }
  179. static int image_pre_load_sig_check_img_sig_sha256(struct image_sig_info *info, ulong addr)
  180. {
  181. struct sig_header_s *sig_header;
  182. u32 header_size, offset_img_sig;
  183. void *header;
  184. u8 sha256_img_sig[SHA256_SUM_LEN];
  185. int ret = 0;
  186. sig_header = (struct sig_header_s *)map_sysmem(addr, SIG_HEADER_LEN);
  187. if (!sig_header) {
  188. log_err("ERROR: can't map first header\n");
  189. ret = -EFAULT;
  190. goto out;
  191. }
  192. header_size = fdt32_to_cpu(sig_header->header_size);
  193. offset_img_sig = fdt32_to_cpu(sig_header->offset_img_sig);
  194. header = (void *)map_sysmem(addr, header_size);
  195. if (!header) {
  196. log_err("ERROR: can't map header\n");
  197. ret = -EFAULT;
  198. goto out_sig_header;
  199. }
  200. sha256_csum_wd(header + offset_img_sig, info->sig_size,
  201. sha256_img_sig, CHUNKSZ_SHA256);
  202. ret = memcmp(sig_header->sha256_img_sig, sha256_img_sig, SHA256_SUM_LEN);
  203. if (ret) {
  204. log_err("ERROR: sha256 of image signature is invalid\n");
  205. ret = -EFAULT;
  206. goto out_header;
  207. }
  208. out_header:
  209. unmap_sysmem(header);
  210. out_sig_header:
  211. unmap_sysmem(sig_header);
  212. out:
  213. return ret;
  214. }
  215. static int image_pre_load_sig_check_img_sig(struct image_sig_info *info, ulong addr)
  216. {
  217. struct sig_header_s *sig_header;
  218. u32 header_size, image_size, offset_img_sig;
  219. void *image;
  220. struct image_region reg;
  221. u32 sig_len;
  222. u8 *sig;
  223. int ret = 0;
  224. sig_header = (struct sig_header_s *)map_sysmem(addr, SIG_HEADER_LEN);
  225. if (!sig_header) {
  226. log_err("ERROR: can't map first header\n");
  227. ret = -EFAULT;
  228. goto out;
  229. }
  230. header_size = fdt32_to_cpu(sig_header->header_size);
  231. image_size = fdt32_to_cpu(sig_header->image_size);
  232. offset_img_sig = fdt32_to_cpu(sig_header->offset_img_sig);
  233. unmap_sysmem(sig_header);
  234. image = (void *)map_sysmem(addr, header_size + image_size);
  235. if (!image) {
  236. log_err("ERROR: can't map full image\n");
  237. ret = -EFAULT;
  238. goto out;
  239. }
  240. reg.data = image + header_size;
  241. reg.size = image_size;
  242. sig = (uint8_t *)image + offset_img_sig;
  243. sig_len = info->sig_size;
  244. ret = info->sig_info.crypto->verify(&info->sig_info, &reg, 1, sig, sig_len);
  245. if (ret) {
  246. log_err("ERROR: signature check has failed (err=%d)\n", ret);
  247. ret = -EINVAL;
  248. goto out_unmap_image;
  249. }
  250. log_info("INFO: signature check has succeed\n");
  251. out_unmap_image:
  252. unmap_sysmem(image);
  253. out:
  254. return ret;
  255. }
  256. int image_pre_load_sig(ulong addr)
  257. {
  258. struct image_sig_info info;
  259. int ret;
  260. ret = image_pre_load_sig_setup(&info);
  261. if (ret < 0)
  262. goto out;
  263. if (ret > 0) {
  264. ret = 0;
  265. goto out;
  266. }
  267. ret = image_pre_load_sig_check_magic(&info, addr);
  268. if (ret < 0)
  269. goto out;
  270. if (ret > 0) {
  271. ret = 0;
  272. goto out;
  273. }
  274. /* Check the signature of the signature header */
  275. ret = image_pre_load_sig_check_header_sig(&info, addr);
  276. if (ret < 0)
  277. goto out;
  278. /* Check sha256 of the image signature */
  279. ret = image_pre_load_sig_check_img_sig_sha256(&info, addr);
  280. if (ret < 0)
  281. goto out;
  282. /* Check the image signature */
  283. ret = image_pre_load_sig_check_img_sig(&info, addr);
  284. if (!ret) {
  285. u32 header_size;
  286. ret = image_pre_load_sig_get_header_size(addr, &header_size);
  287. if (ret) {
  288. log_err("%s: can't get header size\n", __func__);
  289. ret = -EINVAL;
  290. goto out;
  291. }
  292. image_load_offset += header_size;
  293. }
  294. out:
  295. return ret;
  296. }
  297. int image_pre_load(ulong addr)
  298. {
  299. int ret = 0;
  300. image_load_offset = 0;
  301. if (CONFIG_IS_ENABLED(IMAGE_PRE_LOAD_SIG))
  302. ret = image_pre_load_sig(addr);
  303. return ret;
  304. }