avb_persist.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2019. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. *
  31. * The following software/firmware and/or related documentation ("MediaTek Software")
  32. * have been modified by MediaTek Inc. All revisions are subject to any receiver\'s
  33. * applicable license agreements with MediaTek Inc.
  34. */
  35. #include <string.h>
  36. #include <pal_typedefs.h>
  37. #include <pal_log.h>
  38. #include <pal_assert.h>
  39. #include <verified_boot_error.h>
  40. #include <avb_persist.h>
  41. #include <part_status.h>
  42. #include <avb_slot_verify.h>
  43. #include <avb_ops.h>
  44. #include <avb_util.h>
  45. #include <avb_hal.h>
  46. #include <part_interface.h>
  47. PersistDescriptor *g_persist_des;
  48. uint64_t g_persist_offset = 0;
  49. uint32_t g_init_persist_area = 0;
  50. uint32_t g_pre_alloc_sz = 0;
  51. uint32_t g_update_persist = 0;
  52. uint32_t g_persist_value_total_sz = 0;
  53. bool save_to_add(uint32_t ori, uint32_t value_to_add, uint32_t boundary)
  54. {
  55. if ((ori + value_to_add) > boundary || (ori + value_to_add) < ori)
  56. return 0;
  57. return 1;
  58. }
  59. void set_update_persist_status(void)
  60. {
  61. g_update_persist = 1;
  62. }
  63. bool get_update_persist_status(void)
  64. {
  65. return g_update_persist;
  66. }
  67. bool get_init_persist_status(void)
  68. {
  69. if (g_init_persist_area)
  70. return 1;
  71. else
  72. return 0;
  73. }
  74. void avb_persist_descriptor_to_host_byte_order(PersistDescriptorHdr *buf, PersistDescriptorHdr *des)
  75. {
  76. if (buf == NULL || des == NULL)
  77. return;
  78. avb_memcpy(des, buf, sizeof(PersistDescriptorHdr));
  79. des->tag = avb_be32toh(des->tag);
  80. des->key_sz = avb_be32toh(des->key_sz);
  81. des->value_sz = avb_be32toh(des->value_sz);
  82. }
  83. void avb_persist_descriptor_to_slave_byte_order(PersistDescriptorHdr *buf, PersistDescriptorHdr *des)
  84. {
  85. if (buf == NULL || des == NULL)
  86. return;
  87. avb_memcpy(des, buf, sizeof(PersistDescriptorHdr));
  88. des->tag = avb_htobe32(des->tag);
  89. des->key_sz = avb_htobe32(des->key_sz);
  90. des->value_sz = avb_htobe32(des->value_sz);
  91. }
  92. void avb_persist_header_to_host_byte_order(PersistHdr *buf, PersistHdr *h)
  93. {
  94. if (buf == NULL || h == NULL)
  95. return;
  96. avb_memcpy(h, buf, sizeof(h->info));
  97. h->info.magic = avb_be32toh(h->info.magic);
  98. h->info.major_ver = avb_be32toh(h->info.major_ver);
  99. h->info.minor_ver = avb_be32toh(h->info.minor_ver);
  100. }
  101. static uint32_t persist_value_validate(PersistHdr *buf, uint32_t *exist)
  102. {
  103. uint32_t ret = 0;
  104. PersistHdr h;
  105. if (buf == NULL || exist == NULL) {
  106. ret = ERR_VB_INVALID_ADDR;
  107. goto end;
  108. }
  109. avb_persist_header_to_host_byte_order(buf, &h);
  110. *exist = 0;
  111. if (h.info.magic == PERSIST_MAGIC)
  112. *exist = 1;
  113. end:
  114. return ret;
  115. }
  116. uint32_t create_persist_area(AvbOps *ops, char *part_name, uint64_t abs_offset, char *buf)
  117. {
  118. uint32_t ret = 0;
  119. PersistHdr *h = NULL;
  120. AvbIOResult io_res;
  121. if (part_name == NULL || buf == NULL) {
  122. ret = ERR_VB_INVALID_ADDR;
  123. goto end;
  124. }
  125. avb_memset(buf, 0x0, sizeof(PersistHdr));
  126. h = (PersistHdr *)buf;
  127. h->info.magic = avb_htobe32(PERSIST_MAGIC);
  128. h->info.major_ver = avb_htobe32(PERSIST_MAJOR_VER);
  129. h->info.minor_ver = avb_htobe32(PERSIST_MINOR_VER);
  130. io_res = ops->write_to_partition(ops,
  131. part_name,
  132. abs_offset,
  133. sizeof(PersistHdr),
  134. h);
  135. if (io_res != AVB_IO_RESULT_OK) {
  136. pal_log_err("[AVB] Fail to write init persist value to %s.\n", part_name);
  137. ret = (uint32_t)io_res;
  138. goto end;
  139. }
  140. end:
  141. return ret;
  142. }
  143. static uint32_t parse_persist_value(char *buf, uint32_t boundary)
  144. {
  145. uint32_t ret = 0;
  146. #if defined(MTK_SECURITY_SW_SUPPORT) && defined(AVB_PERSIST_VALUE_SUPPORT)
  147. PersistDescriptorHdr *pDes = NULL, info;
  148. uint32_t persist_des_idx = 0;
  149. uint32_t curr_locate = 0;
  150. if (buf == NULL) {
  151. ret = ERR_VB_INVALID_ADDR;
  152. goto end;
  153. }
  154. if (!save_to_add(curr_locate, sizeof(PersistHdr) + \
  155. sizeof(PersistDescriptorHdr), boundary))
  156. goto end;
  157. pDes = (PersistDescriptorHdr *)(buf + sizeof(PersistHdr));
  158. avb_persist_descriptor_to_host_byte_order(pDes, &info);
  159. curr_locate += sizeof(PersistHdr) + sizeof(PersistDescriptorHdr);
  160. while (info.tag == PERSIST_TAG_MAGIC) {
  161. g_persist_des[persist_des_idx].hdr.tag = info.tag;
  162. g_persist_des[persist_des_idx].hdr.key_sz = info.key_sz;
  163. g_persist_des[persist_des_idx].hdr.value_sz = info.value_sz;
  164. g_persist_des[persist_des_idx].key = avb_malloc(info.key_sz);
  165. if (g_persist_des[persist_des_idx].key == NULL) {
  166. ret = ERR_VB_INVALID_ADDR;
  167. goto end;
  168. }
  169. if (!save_to_add(curr_locate, info.key_sz, boundary)) {
  170. ret = ERR_VB_BUF_OVERFLOW;
  171. goto end;
  172. }
  173. avb_memcpy(g_persist_des[persist_des_idx].key, (buf + curr_locate), info.key_sz);
  174. curr_locate += info.key_sz;
  175. g_persist_des[persist_des_idx].value = avb_malloc(info.value_sz);
  176. if (g_persist_des[persist_des_idx].value == NULL) {
  177. ret = ERR_VB_INVALID_ADDR;
  178. goto end;
  179. }
  180. if (!save_to_add(curr_locate, info.value_sz, boundary)) {
  181. ret = ERR_VB_BUF_OVERFLOW;
  182. goto end;
  183. }
  184. avb_memcpy(g_persist_des[persist_des_idx].value, (buf + curr_locate), info.value_sz);
  185. curr_locate += info.value_sz;
  186. if (!save_to_add(curr_locate, sizeof(PersistDescriptorHdr), boundary))
  187. goto end;
  188. pDes = (PersistDescriptorHdr *)(buf + curr_locate);
  189. avb_persist_descriptor_to_host_byte_order(pDes, &info);
  190. curr_locate += sizeof(PersistDescriptorHdr);
  191. persist_des_idx++;
  192. }
  193. g_persist_value_total_sz = curr_locate - sizeof(PersistHdr);
  194. end:
  195. #endif
  196. return ret;
  197. }
  198. uint32_t write_persist_value(AvbOps *ops, char *part_name)
  199. {
  200. uint32_t ret = 0;
  201. #if defined(MTK_SECURITY_SW_SUPPORT) && defined(AVB_PERSIST_VALUE_SUPPORT)
  202. uint32_t persist_des_idx = 0;
  203. uint64_t curr_offset = 0;
  204. uint64_t part_sz = 0;
  205. uint64_t total_write_sz = 0;
  206. uint64_t redundant_sz = 0;
  207. uint8_t *tmp_buf = NULL;
  208. AvbIOResult io_res;
  209. PersistDescriptorHdr info;
  210. if (!get_init_persist_status()) {
  211. ret = ERR_AVB_UNINITIAL_PERSIST_VALUE;
  212. goto end;
  213. }
  214. if (!get_update_persist_status())
  215. goto end;
  216. /* get persist storage size in specific "partition" */
  217. part_sz = partition_get_size_by_name(part_name);
  218. if (part_sz == PART_NOT_EXIST) {
  219. ret = ERR_AVB_NO_SUCH_PARTITION;
  220. goto end;
  221. }
  222. if (!save_to_add(curr_offset, g_persist_offset + \
  223. sizeof(PersistHdr), part_sz)) {
  224. ret = ERR_VB_BUF_OVERFLOW;
  225. goto end;
  226. }
  227. curr_offset += (g_persist_offset + sizeof(PersistHdr));
  228. while (persist_des_idx < MAX_PERSIST_TAG_SZ) {
  229. if (g_persist_des[persist_des_idx].hdr.tag != PERSIST_TAG_MAGIC) {
  230. persist_des_idx++;
  231. continue;
  232. }
  233. avb_persist_descriptor_to_slave_byte_order(
  234. &g_persist_des[persist_des_idx].hdr, &info);
  235. io_res = ops->write_to_partition(ops,
  236. part_name,
  237. curr_offset,
  238. sizeof(PersistDescriptorHdr),
  239. &info);
  240. if (io_res != AVB_IO_RESULT_OK) {
  241. pal_log_err("Error writing to partition %s.\n", part_name);
  242. ret = (uint32_t)io_res;
  243. goto end;
  244. }
  245. curr_offset += sizeof(PersistDescriptorHdr);
  246. if (!save_to_add(curr_offset,
  247. g_persist_des[persist_des_idx].hdr.key_sz, part_sz)) {
  248. ret = ERR_VB_BUF_OVERFLOW;
  249. goto end;
  250. }
  251. io_res = ops->write_to_partition(ops,
  252. part_name,
  253. curr_offset,
  254. g_persist_des[persist_des_idx].hdr.key_sz,
  255. g_persist_des[persist_des_idx].key);
  256. if (io_res != AVB_IO_RESULT_OK) {
  257. pal_log_err("Error writing to partition %s.\n", part_name);
  258. ret = (uint32_t)io_res;
  259. goto end;
  260. }
  261. curr_offset += g_persist_des[persist_des_idx].hdr.key_sz;
  262. if (!save_to_add(curr_offset,
  263. g_persist_des[persist_des_idx].hdr.value_sz,
  264. part_sz)) {
  265. ret = ERR_VB_BUF_OVERFLOW;
  266. goto end;
  267. }
  268. io_res = ops->write_to_partition(ops,
  269. part_name,
  270. curr_offset,
  271. g_persist_des[persist_des_idx].hdr.value_sz,
  272. g_persist_des[persist_des_idx].value);
  273. if (io_res != AVB_IO_RESULT_OK) {
  274. pal_log_err("Error writing to partition %s.\n", part_name);
  275. ret = (uint32_t)io_res;
  276. goto end;
  277. }
  278. curr_offset += g_persist_des[persist_des_idx].hdr.value_sz;
  279. persist_des_idx++;
  280. }
  281. total_write_sz = curr_offset - g_persist_offset - sizeof(PersistHdr);
  282. if (total_write_sz < g_persist_value_total_sz) {
  283. redundant_sz = g_persist_value_total_sz - total_write_sz;
  284. tmp_buf = avb_malloc(redundant_sz);
  285. if (tmp_buf == NULL) {
  286. ret = AVB_IO_RESULT_ERROR_OOM;
  287. goto end;
  288. }
  289. avb_memset(tmp_buf, 0x0, redundant_sz);
  290. io_res = ops->write_to_partition(ops,
  291. part_name,
  292. curr_offset,
  293. redundant_sz,
  294. tmp_buf);
  295. if (io_res != AVB_IO_RESULT_OK) {
  296. pal_log_err("Error writing to partition %s.\n", part_name);
  297. ret = (uint32_t)io_res;
  298. goto end;
  299. }
  300. }
  301. pal_log_info("Write persist value to %s.\n", part_name);
  302. end:
  303. if (tmp_buf != NULL)
  304. avb_free(tmp_buf);
  305. #endif
  306. return ret;
  307. }
  308. uint32_t init_persist_value(AvbOps *ops, char *part_name, uint64_t offset)
  309. {
  310. uint32_t ret = 0;
  311. #if defined(MTK_SECURITY_SW_SUPPORT) && defined(AVB_PERSIST_VALUE_SUPPORT)
  312. uint64_t part_sz = 0;
  313. size_t read_len = 0;
  314. uint64_t abs_offset = 0;
  315. PersistHdr *buf = NULL;
  316. AvbIOResult io_res;
  317. uint32_t persist_exist = 0;
  318. if (part_name == NULL) {
  319. ret = ERR_VB_INVALID_ADDR;
  320. goto end;
  321. }
  322. /* get persist storage offset in specific "partition" */
  323. part_sz = partition_get_size_by_name(part_name);
  324. if (part_sz == PART_NOT_EXIST) {
  325. ret = ERR_AVB_NO_SUCH_PARTITION;
  326. goto end;
  327. }
  328. /* Check offset value. If offset is uninitial, we assume that persist
  329. * value is stored at the offset is half of assigned partition size.
  330. */
  331. g_persist_offset = offset;
  332. if (g_persist_offset == PERSIST_UNSPECIFIED_OFFSET) {
  333. pal_log_info("[AVB] Persist offset is half of partition \"%s\" size\n",
  334. part_name);
  335. g_persist_offset = ((part_sz / 2) + (PERSIST_ALIGN_SZ - 1)) /
  336. PERSIST_ALIGN_SZ * PERSIST_ALIGN_SZ;
  337. }
  338. if (g_persist_offset >= part_sz ||
  339. (g_persist_offset + sizeof(PersistHdr) + sizeof(PersistDescriptor)) > part_sz ||
  340. (g_persist_offset + sizeof(PersistHdr) + sizeof(PersistDescriptor)) <= g_persist_offset) {
  341. pal_log_err("[AVB] persist_offset = 0x%llx is invalid\n", g_persist_offset);
  342. ret = ERR_AVB_INVALID_OFFSET;
  343. goto end;
  344. }
  345. ret = (uint32_t)get_abs_offset(part_name, g_persist_offset, &abs_offset);
  346. if (ret != AVB_IO_RESULT_OK)
  347. goto end;
  348. /* Read persist area. If it does not exist, create a new one. */
  349. if ((part_sz - abs_offset) > DEFAULT_PERSIST_PREALLOC_BUF_SZ)
  350. g_pre_alloc_sz = DEFAULT_PERSIST_PREALLOC_BUF_SZ;
  351. else
  352. g_pre_alloc_sz = part_sz - abs_offset;
  353. buf = avb_malloc(g_pre_alloc_sz);
  354. if (buf == NULL) {
  355. ret = ERR_VB_INVALID_ADDR;
  356. goto end;
  357. }
  358. io_res = ops->read_from_partition(ops,
  359. part_name,
  360. abs_offset,
  361. g_pre_alloc_sz,
  362. buf,
  363. &read_len);
  364. if (io_res != AVB_IO_RESULT_OK) {
  365. pal_log_err("Error loading persist value from %s.\n", part_name);
  366. ret = (uint32_t)io_res;
  367. goto end;
  368. }
  369. ret = persist_value_validate(buf, &persist_exist);
  370. if (ret)
  371. goto end;
  372. g_persist_des = avb_malloc(sizeof(PersistDescriptor) * MAX_PERSIST_TAG_SZ);
  373. if (g_persist_des == NULL) {
  374. pal_log_err("g_persist_des NULL\n");
  375. ret = ERR_VB_INVALID_ADDR;
  376. goto end;
  377. }
  378. avb_memset(g_persist_des, 0x0, sizeof(PersistDescriptor) * MAX_PERSIST_TAG_SZ);
  379. if (!persist_exist) {
  380. ret = create_persist_area(ops, part_name, abs_offset, (char *)buf);
  381. if (ret)
  382. goto end;
  383. } else {
  384. ret = parse_persist_value((char *)buf, g_pre_alloc_sz);
  385. if (ret) {
  386. pal_log_err("Parse persist value fail. ret = 0x%x\n", ret);
  387. avb_memset(g_persist_des, 0x0, sizeof(PersistDescriptor) * MAX_PERSIST_TAG_SZ);
  388. ret = 0;
  389. }
  390. }
  391. g_init_persist_area = 1;
  392. end:
  393. pal_log_info("[AVB] \"%s\" partition size = 0x%llx\n", part_name, part_sz);
  394. pal_log_info("[AVB] persist offset = 0x%llx\n", g_persist_offset);
  395. pal_log_info("[AVB] persist allocate size = 0x%x\n", g_pre_alloc_sz);
  396. pal_log_info("[AVB] persist area init end (%d).\n", g_init_persist_area);
  397. pal_log_info("[AVB] persist ret = 0x%x.\n", ret);
  398. if (buf != NULL)
  399. avb_free(buf);
  400. #endif
  401. return ret;
  402. }