avb_verify.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. /*
  2. * (C) Copyright 2018, Linaro Limited
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <avb_verify.h>
  7. #include <blk.h>
  8. #include <cpu_func.h>
  9. #include <fastboot.h>
  10. #include <image.h>
  11. #include <malloc.h>
  12. #include <part.h>
  13. #include <tee.h>
  14. #include <tee/optee_ta_avb.h>
  15. static const unsigned char avb_root_pub[1032] = {
  16. 0x0, 0x0, 0x10, 0x0, 0x55, 0xd9, 0x4, 0xad, 0xd8, 0x4,
  17. 0xaf, 0xe3, 0xd3, 0x84, 0x6c, 0x7e, 0xd, 0x89, 0x3d, 0xc2,
  18. 0x8c, 0xd3, 0x12, 0x55, 0xe9, 0x62, 0xc9, 0xf1, 0xf, 0x5e,
  19. 0xcc, 0x16, 0x72, 0xab, 0x44, 0x7c, 0x2c, 0x65, 0x4a, 0x94,
  20. 0xb5, 0x16, 0x2b, 0x0, 0xbb, 0x6, 0xef, 0x13, 0x7, 0x53,
  21. 0x4c, 0xf9, 0x64, 0xb9, 0x28, 0x7a, 0x1b, 0x84, 0x98, 0x88,
  22. 0xd8, 0x67, 0xa4, 0x23, 0xf9, 0xa7, 0x4b, 0xdc, 0x4a, 0xf,
  23. 0xf7, 0x3a, 0x18, 0xae, 0x54, 0xa8, 0x15, 0xfe, 0xb0, 0xad,
  24. 0xac, 0x35, 0xda, 0x3b, 0xad, 0x27, 0xbc, 0xaf, 0xe8, 0xd3,
  25. 0x2f, 0x37, 0x34, 0xd6, 0x51, 0x2b, 0x6c, 0x5a, 0x27, 0xd7,
  26. 0x96, 0x6, 0xaf, 0x6b, 0xb8, 0x80, 0xca, 0xfa, 0x30, 0xb4,
  27. 0xb1, 0x85, 0xb3, 0x4d, 0xaa, 0xaa, 0xc3, 0x16, 0x34, 0x1a,
  28. 0xb8, 0xe7, 0xc7, 0xfa, 0xf9, 0x9, 0x77, 0xab, 0x97, 0x93,
  29. 0xeb, 0x44, 0xae, 0xcf, 0x20, 0xbc, 0xf0, 0x80, 0x11, 0xd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47. 0xd8, 0x79, 0x45, 0xff, 0x3, 0x4c, 0x26, 0xa2, 0x0, 0x5a,
  48. 0x8e, 0xc2, 0x51, 0xa1, 0x15, 0xf9, 0x7b, 0xf4, 0x5c, 0x81,
  49. 0x9b, 0x18, 0x47, 0x35, 0xd8, 0x2d, 0x5, 0xe9, 0xad, 0xf,
  50. 0x35, 0x74, 0x15, 0xa3, 0x8e, 0x8b, 0xcc, 0x27, 0xda, 0x7c,
  51. 0x5d, 0xe4, 0xfa, 0x4, 0xd3, 0x5, 0xb, 0xba, 0x3a, 0xb2,
  52. 0x49, 0x45, 0x2f, 0x47, 0xc7, 0xd, 0x41, 0x3f, 0x97, 0x80,
  53. 0x4d, 0x3f, 0xc1, 0xb5, 0xbb, 0x70, 0x5f, 0xa7, 0x37, 0xaf,
  54. 0x48, 0x22, 0x12, 0x45, 0x2e, 0xf5, 0xf, 0x87, 0x92, 0xe2,
  55. 0x84, 0x1, 0xf9, 0x12, 0xf, 0x14, 0x15, 0x24, 0xce, 0x89,
  56. 0x99, 0xee, 0xb9, 0xc4, 0x17, 0x70, 0x70, 0x15, 0xea, 0xbe,
  57. 0xc6, 0x6c, 0x1f, 0x62, 0xb3, 0xf4, 0x2d, 0x16, 0x87, 0xfb,
  58. 0x56, 0x1e, 0x45, 0xab, 0xae, 0x32, 0xe4, 0x5e, 0x91, 0xed,
  59. 0x53, 0x66, 0x5e, 0xbd, 0xed, 0xad, 0xe6, 0x12, 0x39, 0xd,
  60. 0x83, 0xc9, 0xe8, 0x6b, 0x6c, 0x2d, 0xa5, 0xee, 0xc4, 0x5a,
  61. 0x66, 0xae, 0x8c, 0x97, 0xd7, 0xd, 0x6c, 0x49, 0xc7, 0xf5,
  62. 0xc4, 0x92, 0x31, 0x8b, 0x9, 0xee, 0x33, 0xda, 0xa9, 0x37,
  63. 0xb6, 0x49, 0x18, 0xf8, 0xe, 0x60, 0x45, 0xc8, 0x33, 0x91,
  64. 0xef, 0x20, 0x57, 0x10, 0xbe, 0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,
  99. 0x79, 0x21, 0xdf, 0xd7, 0x31, 0x30, 0xc4, 0x21, 0xdd, 0x34,
  100. 0x5b, 0xd2, 0x1a, 0x2b, 0x3e, 0x5d, 0xf7, 0xea, 0xca, 0x5,
  101. 0x8e, 0xb7, 0xcb, 0x49, 0x2e, 0xa0, 0xe3, 0xf4, 0xa7, 0x48,
  102. 0x19, 0x10, 0x9c, 0x4, 0xa7, 0xf4, 0x28, 0x74, 0xc8, 0x6f,
  103. 0x63, 0x20, 0x2b, 0x46, 0x24, 0x26, 0x19, 0x1d, 0xd1, 0x2c,
  104. 0x31, 0x6d, 0x5a, 0x29, 0xa2, 0x6, 0xa6, 0xb2, 0x41, 0xcc,
  105. 0xa, 0x27, 0x96, 0x9, 0x96, 0xac, 0x47, 0x65, 0x78, 0x68,
  106. 0x51, 0x98, 0xd6, 0xd8, 0xa6, 0x2d, 0xa0, 0xcf, 0xec, 0xe2,
  107. 0x74, 0xf2, 0x82, 0xe3, 0x97, 0xd9, 0x7e, 0xd4, 0xf8, 0xb,
  108. 0x70, 0x43, 0x3d, 0xb1, 0x7b, 0x97, 0x80, 0xd6, 0xcb, 0xd7,
  109. 0x19, 0xbc, 0x63, 0xb, 0xfd, 0x4d, 0x88, 0xfe, 0x67, 0xac,
  110. 0xb8, 0xcc, 0x50, 0xb7, 0x68, 0xb3, 0x5b, 0xd6, 0x1e, 0x25,
  111. 0xfc, 0x5f, 0x3c, 0x8d, 0xb1, 0x33, 0x7c, 0xb3, 0x49, 0x1,
  112. 0x3f, 0x71, 0x55, 0xe, 0x51, 0xba, 0x61, 0x26, 0xfa, 0xea,
  113. 0xe5, 0xb5, 0xe8, 0xaa, 0xcf, 0xcd, 0x96, 0x9f, 0xd6, 0xc1,
  114. 0x5f, 0x53, 0x91, 0xad, 0x5, 0xde, 0x20, 0xe7, 0x51, 0xda,
  115. 0x5b, 0x95, 0x67, 0xed, 0xf4, 0xee, 0x42, 0x65, 0x70, 0x13,
  116. 0xb, 0x70, 0x14, 0x1c, 0xc9, 0xe0, 0x19, 0xca, 0x5f, 0xf5,
  117. 0x1d, 0x70, 0x4b, 0x6c, 0x6, 0x74, 0xec, 0xb5, 0x2e, 0x77,
  118. 0xe1, 0x74, 0xa1, 0xa3, 0x99, 0xa0, 0x85, 0x9e, 0xf1, 0xac,
  119. 0xd8, 0x7e,
  120. };
  121. /**
  122. * ============================================================================
  123. * Boot states support (GREEN, YELLOW, ORANGE, RED) and dm_verity
  124. * ============================================================================
  125. */
  126. char *avb_set_state(AvbOps *ops, enum avb_boot_state boot_state)
  127. {
  128. struct AvbOpsData *data;
  129. char *cmdline = NULL;
  130. if (!ops)
  131. return NULL;
  132. data = (struct AvbOpsData *)ops->user_data;
  133. if (!data)
  134. return NULL;
  135. data->boot_state = boot_state;
  136. switch (boot_state) {
  137. case AVB_GREEN:
  138. cmdline = "androidboot.verifiedbootstate=green";
  139. break;
  140. case AVB_YELLOW:
  141. cmdline = "androidboot.verifiedbootstate=yellow";
  142. break;
  143. case AVB_ORANGE:
  144. cmdline = "androidboot.verifiedbootstate=orange";
  145. case AVB_RED:
  146. break;
  147. }
  148. return cmdline;
  149. }
  150. char *append_cmd_line(char *cmdline_orig, char *cmdline_new)
  151. {
  152. char *cmd_line;
  153. if (!cmdline_new)
  154. return cmdline_orig;
  155. if (cmdline_orig)
  156. cmd_line = cmdline_orig;
  157. else
  158. cmd_line = " ";
  159. cmd_line = avb_strdupv(cmd_line, " ", cmdline_new, NULL);
  160. return cmd_line;
  161. }
  162. static int avb_find_dm_args(char **args, char *str)
  163. {
  164. int i;
  165. if (!str)
  166. return -1;
  167. for (i = 0; i < AVB_MAX_ARGS && args[i]; ++i) {
  168. if (strstr(args[i], str))
  169. return i;
  170. }
  171. return -1;
  172. }
  173. static char *avb_set_enforce_option(const char *cmdline, const char *option)
  174. {
  175. char *cmdarg[AVB_MAX_ARGS];
  176. char *newargs = NULL;
  177. int i = 0;
  178. int total_args;
  179. memset(cmdarg, 0, sizeof(cmdarg));
  180. cmdarg[i++] = strtok((char *)cmdline, " ");
  181. do {
  182. cmdarg[i] = strtok(NULL, " ");
  183. if (!cmdarg[i])
  184. break;
  185. if (++i >= AVB_MAX_ARGS) {
  186. printf("%s: Can't handle more then %d args\n",
  187. __func__, i);
  188. return NULL;
  189. }
  190. } while (true);
  191. total_args = i;
  192. i = avb_find_dm_args(&cmdarg[0], VERITY_TABLE_OPT_LOGGING);
  193. if (i >= 0) {
  194. cmdarg[i] = (char *)option;
  195. } else {
  196. i = avb_find_dm_args(&cmdarg[0], VERITY_TABLE_OPT_RESTART);
  197. if (i < 0) {
  198. printf("%s: No verity options found\n", __func__);
  199. return NULL;
  200. }
  201. cmdarg[i] = (char *)option;
  202. }
  203. for (i = 0; i <= total_args; i++)
  204. newargs = append_cmd_line(newargs, cmdarg[i]);
  205. return newargs;
  206. }
  207. char *avb_set_ignore_corruption(const char *cmdline)
  208. {
  209. char *newargs = NULL;
  210. newargs = avb_set_enforce_option(cmdline, VERITY_TABLE_OPT_LOGGING);
  211. if (newargs)
  212. newargs = append_cmd_line(newargs,
  213. "androidboot.veritymode=eio");
  214. return newargs;
  215. }
  216. char *avb_set_enforce_verity(const char *cmdline)
  217. {
  218. char *newargs;
  219. newargs = avb_set_enforce_option(cmdline, VERITY_TABLE_OPT_RESTART);
  220. if (newargs)
  221. newargs = append_cmd_line(newargs,
  222. "androidboot.veritymode=enforcing");
  223. return newargs;
  224. }
  225. /**
  226. * ============================================================================
  227. * IO(mmc) auxiliary functions
  228. * ============================================================================
  229. */
  230. static unsigned long mmc_read_and_flush(struct mmc_part *part,
  231. lbaint_t start,
  232. lbaint_t sectors,
  233. void *buffer)
  234. {
  235. unsigned long blks;
  236. void *tmp_buf;
  237. size_t buf_size;
  238. bool unaligned = is_buf_unaligned(buffer);
  239. if (start < part->info.start) {
  240. printf("%s: partition start out of bounds\n", __func__);
  241. return 0;
  242. }
  243. if ((start + sectors) > (part->info.start + part->info.size)) {
  244. sectors = part->info.start + part->info.size - start;
  245. printf("%s: read sector aligned to partition bounds (%ld)\n",
  246. __func__, sectors);
  247. }
  248. /*
  249. * Reading fails on unaligned buffers, so we have to
  250. * use aligned temporary buffer and then copy to destination
  251. */
  252. if (unaligned) {
  253. printf("Handling unaligned read buffer..\n");
  254. tmp_buf = get_sector_buf();
  255. buf_size = get_sector_buf_size();
  256. if (sectors > buf_size / part->info.blksz)
  257. sectors = buf_size / part->info.blksz;
  258. } else {
  259. tmp_buf = buffer;
  260. }
  261. blks = blk_dread(part->mmc_blk,
  262. start, sectors, tmp_buf);
  263. /* flush cache after read */
  264. flush_cache((ulong)tmp_buf, sectors * part->info.blksz);
  265. if (unaligned)
  266. memcpy(buffer, tmp_buf, sectors * part->info.blksz);
  267. return blks;
  268. }
  269. static unsigned long mmc_write(struct mmc_part *part, lbaint_t start,
  270. lbaint_t sectors, void *buffer)
  271. {
  272. void *tmp_buf;
  273. size_t buf_size;
  274. bool unaligned = is_buf_unaligned(buffer);
  275. if (start < part->info.start) {
  276. printf("%s: partition start out of bounds\n", __func__);
  277. return 0;
  278. }
  279. if ((start + sectors) > (part->info.start + part->info.size)) {
  280. sectors = part->info.start + part->info.size - start;
  281. printf("%s: sector aligned to partition bounds (%ld)\n",
  282. __func__, sectors);
  283. }
  284. if (unaligned) {
  285. tmp_buf = get_sector_buf();
  286. buf_size = get_sector_buf_size();
  287. printf("Handling unaligned wrire buffer..\n");
  288. if (sectors > buf_size / part->info.blksz)
  289. sectors = buf_size / part->info.blksz;
  290. memcpy(tmp_buf, buffer, sectors * part->info.blksz);
  291. } else {
  292. tmp_buf = buffer;
  293. }
  294. return blk_dwrite(part->mmc_blk,
  295. start, sectors, tmp_buf);
  296. }
  297. static struct mmc_part *get_partition(AvbOps *ops, const char *partition)
  298. {
  299. int ret;
  300. u8 dev_num;
  301. int part_num = 0;
  302. struct mmc_part *part;
  303. struct blk_desc *mmc_blk;
  304. part = malloc(sizeof(struct mmc_part));
  305. if (!part)
  306. return NULL;
  307. dev_num = get_boot_device(ops);
  308. part->mmc = find_mmc_device(dev_num);
  309. if (!part->mmc) {
  310. printf("No MMC device at slot %x\n", dev_num);
  311. goto err;
  312. }
  313. if (mmc_init(part->mmc)) {
  314. printf("MMC initialization failed\n");
  315. goto err;
  316. }
  317. ret = mmc_switch_part(part->mmc, part_num);
  318. if (ret)
  319. goto err;
  320. mmc_blk = mmc_get_blk_desc(part->mmc);
  321. if (!mmc_blk) {
  322. printf("Error - failed to obtain block descriptor\n");
  323. goto err;
  324. }
  325. ret = part_get_info_by_name(mmc_blk, partition, &part->info);
  326. if (!ret) {
  327. printf("Can't find partition '%s'\n", partition);
  328. goto err;
  329. }
  330. part->dev_num = dev_num;
  331. part->mmc_blk = mmc_blk;
  332. return part;
  333. err:
  334. free(part);
  335. return NULL;
  336. }
  337. static AvbIOResult mmc_byte_io(AvbOps *ops,
  338. const char *partition,
  339. s64 offset,
  340. size_t num_bytes,
  341. void *buffer,
  342. size_t *out_num_read,
  343. enum mmc_io_type io_type)
  344. {
  345. ulong ret;
  346. struct mmc_part *part;
  347. u64 start_offset, start_sector, sectors, residue;
  348. u8 *tmp_buf;
  349. size_t io_cnt = 0;
  350. if (!partition || !buffer || io_type > IO_WRITE)
  351. return AVB_IO_RESULT_ERROR_IO;
  352. part = get_partition(ops, partition);
  353. if (!part)
  354. return AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION;
  355. if (!part->info.blksz)
  356. return AVB_IO_RESULT_ERROR_IO;
  357. start_offset = calc_offset(part, offset);
  358. while (num_bytes) {
  359. start_sector = start_offset / part->info.blksz;
  360. sectors = num_bytes / part->info.blksz;
  361. /* handle non block-aligned reads */
  362. if (start_offset % part->info.blksz ||
  363. num_bytes < part->info.blksz) {
  364. tmp_buf = get_sector_buf();
  365. if (start_offset % part->info.blksz) {
  366. residue = part->info.blksz -
  367. (start_offset % part->info.blksz);
  368. if (residue > num_bytes)
  369. residue = num_bytes;
  370. } else {
  371. residue = num_bytes;
  372. }
  373. if (io_type == IO_READ) {
  374. ret = mmc_read_and_flush(part,
  375. part->info.start +
  376. start_sector,
  377. 1, tmp_buf);
  378. if (ret != 1) {
  379. printf("%s: read error (%ld, %lld)\n",
  380. __func__, ret, start_sector);
  381. return AVB_IO_RESULT_ERROR_IO;
  382. }
  383. /*
  384. * if this is not aligned at sector start,
  385. * we have to adjust the tmp buffer
  386. */
  387. tmp_buf += (start_offset % part->info.blksz);
  388. memcpy(buffer, (void *)tmp_buf, residue);
  389. } else {
  390. ret = mmc_read_and_flush(part,
  391. part->info.start +
  392. start_sector,
  393. 1, tmp_buf);
  394. if (ret != 1) {
  395. printf("%s: read error (%ld, %lld)\n",
  396. __func__, ret, start_sector);
  397. return AVB_IO_RESULT_ERROR_IO;
  398. }
  399. memcpy((void *)tmp_buf +
  400. start_offset % part->info.blksz,
  401. buffer, residue);
  402. ret = mmc_write(part, part->info.start +
  403. start_sector, 1, tmp_buf);
  404. if (ret != 1) {
  405. printf("%s: write error (%ld, %lld)\n",
  406. __func__, ret, start_sector);
  407. return AVB_IO_RESULT_ERROR_IO;
  408. }
  409. }
  410. io_cnt += residue;
  411. buffer += residue;
  412. start_offset += residue;
  413. num_bytes -= residue;
  414. continue;
  415. }
  416. if (sectors) {
  417. if (io_type == IO_READ) {
  418. ret = mmc_read_and_flush(part,
  419. part->info.start +
  420. start_sector,
  421. sectors, buffer);
  422. } else {
  423. ret = mmc_write(part,
  424. part->info.start +
  425. start_sector,
  426. sectors, buffer);
  427. }
  428. if (!ret) {
  429. printf("%s: sector read error\n", __func__);
  430. return AVB_IO_RESULT_ERROR_IO;
  431. }
  432. io_cnt += ret * part->info.blksz;
  433. buffer += ret * part->info.blksz;
  434. start_offset += ret * part->info.blksz;
  435. num_bytes -= ret * part->info.blksz;
  436. }
  437. }
  438. /* Set counter for read operation */
  439. if (io_type == IO_READ && out_num_read)
  440. *out_num_read = io_cnt;
  441. return AVB_IO_RESULT_OK;
  442. }
  443. /**
  444. * ============================================================================
  445. * AVB 2.0 operations
  446. * ============================================================================
  447. */
  448. /**
  449. * read_from_partition() - reads @num_bytes from @offset from partition
  450. * identified by a string name
  451. *
  452. * @ops: contains AVB ops handlers
  453. * @partition_name: partition name, NUL-terminated UTF-8 string
  454. * @offset: offset from the beginning of partition
  455. * @num_bytes: amount of bytes to read
  456. * @buffer: destination buffer to store data
  457. * @out_num_read:
  458. *
  459. * @return:
  460. * AVB_IO_RESULT_OK, if partition was found and read operation succeed
  461. * AVB_IO_RESULT_ERROR_IO, if i/o error occurred from the underlying i/o
  462. * subsystem
  463. * AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION, if there is no partition with
  464. * the given name
  465. */
  466. static AvbIOResult read_from_partition(AvbOps *ops,
  467. const char *partition_name,
  468. s64 offset_from_partition,
  469. size_t num_bytes,
  470. void *buffer,
  471. size_t *out_num_read)
  472. {
  473. return mmc_byte_io(ops, partition_name, offset_from_partition,
  474. num_bytes, buffer, out_num_read, IO_READ);
  475. }
  476. /**
  477. * write_to_partition() - writes N bytes to a partition identified by a string
  478. * name
  479. *
  480. * @ops: AvbOps, contains AVB ops handlers
  481. * @partition_name: partition name
  482. * @offset_from_partition: offset from the beginning of partition
  483. * @num_bytes: amount of bytes to write
  484. * @buf: data to write
  485. * @out_num_read:
  486. *
  487. * @return:
  488. * AVB_IO_RESULT_OK, if partition was found and read operation succeed
  489. * AVB_IO_RESULT_ERROR_IO, if input/output error occurred
  490. * AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION, if partition, specified in
  491. * @partition_name was not found
  492. */
  493. static AvbIOResult write_to_partition(AvbOps *ops,
  494. const char *partition_name,
  495. s64 offset_from_partition,
  496. size_t num_bytes,
  497. const void *buffer)
  498. {
  499. return mmc_byte_io(ops, partition_name, offset_from_partition,
  500. num_bytes, (void *)buffer, NULL, IO_WRITE);
  501. }
  502. /**
  503. * validate_vmbeta_public_key() - checks if the given public key used to sign
  504. * the vbmeta partition is trusted
  505. *
  506. * @ops: AvbOps, contains AVB ops handlers
  507. * @public_key_data: public key for verifying vbmeta partition signature
  508. * @public_key_length: length of public key
  509. * @public_key_metadata:
  510. * @public_key_metadata_length:
  511. * @out_key_is_trusted:
  512. *
  513. * @return:
  514. * AVB_IO_RESULT_OK, if partition was found and read operation succeed
  515. */
  516. static AvbIOResult validate_vbmeta_public_key(AvbOps *ops,
  517. const u8 *public_key_data,
  518. size_t public_key_length,
  519. const u8
  520. *public_key_metadata,
  521. size_t
  522. public_key_metadata_length,
  523. bool *out_key_is_trusted)
  524. {
  525. if (!public_key_length || !public_key_data || !out_key_is_trusted)
  526. return AVB_IO_RESULT_ERROR_IO;
  527. *out_key_is_trusted = false;
  528. if (public_key_length != sizeof(avb_root_pub))
  529. return AVB_IO_RESULT_ERROR_IO;
  530. if (memcmp(avb_root_pub, public_key_data, public_key_length) == 0)
  531. *out_key_is_trusted = true;
  532. return AVB_IO_RESULT_OK;
  533. }
  534. #ifdef CONFIG_OPTEE_TA_AVB
  535. static int get_open_session(struct AvbOpsData *ops_data)
  536. {
  537. struct udevice *tee = NULL;
  538. while (!ops_data->tee) {
  539. const struct tee_optee_ta_uuid uuid = TA_AVB_UUID;
  540. struct tee_open_session_arg arg;
  541. int rc;
  542. tee = tee_find_device(tee, NULL, NULL, NULL);
  543. if (!tee)
  544. return -ENODEV;
  545. memset(&arg, 0, sizeof(arg));
  546. tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
  547. rc = tee_open_session(tee, &arg, 0, NULL);
  548. if (!rc) {
  549. ops_data->tee = tee;
  550. ops_data->session = arg.session;
  551. }
  552. }
  553. return 0;
  554. }
  555. static AvbIOResult invoke_func(struct AvbOpsData *ops_data, u32 func,
  556. ulong num_param, struct tee_param *param)
  557. {
  558. struct tee_invoke_arg arg;
  559. if (get_open_session(ops_data))
  560. return AVB_IO_RESULT_ERROR_IO;
  561. memset(&arg, 0, sizeof(arg));
  562. arg.func = func;
  563. arg.session = ops_data->session;
  564. if (tee_invoke_func(ops_data->tee, &arg, num_param, param))
  565. return AVB_IO_RESULT_ERROR_IO;
  566. switch (arg.ret) {
  567. case TEE_SUCCESS:
  568. return AVB_IO_RESULT_OK;
  569. case TEE_ERROR_OUT_OF_MEMORY:
  570. return AVB_IO_RESULT_ERROR_OOM;
  571. case TEE_ERROR_STORAGE_NO_SPACE:
  572. return AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE;
  573. case TEE_ERROR_ITEM_NOT_FOUND:
  574. return AVB_IO_RESULT_ERROR_NO_SUCH_VALUE;
  575. case TEE_ERROR_TARGET_DEAD:
  576. /*
  577. * The TA has paniced, close the session to reload the TA
  578. * for the next request.
  579. */
  580. tee_close_session(ops_data->tee, ops_data->session);
  581. ops_data->tee = NULL;
  582. return AVB_IO_RESULT_ERROR_IO;
  583. default:
  584. return AVB_IO_RESULT_ERROR_IO;
  585. }
  586. }
  587. #endif
  588. /**
  589. * read_rollback_index() - gets the rollback index corresponding to the
  590. * location of given by @out_rollback_index.
  591. *
  592. * @ops: contains AvbOps handlers
  593. * @rollback_index_slot:
  594. * @out_rollback_index: used to write a retrieved rollback index.
  595. *
  596. * @return
  597. * AVB_IO_RESULT_OK, if the roolback index was retrieved
  598. */
  599. static AvbIOResult read_rollback_index(AvbOps *ops,
  600. size_t rollback_index_slot,
  601. u64 *out_rollback_index)
  602. {
  603. #ifndef CONFIG_OPTEE_TA_AVB
  604. /* For now we always return 0 as the stored rollback index. */
  605. printf("%s not supported yet\n", __func__);
  606. if (out_rollback_index)
  607. *out_rollback_index = 0;
  608. return AVB_IO_RESULT_OK;
  609. #else
  610. AvbIOResult rc;
  611. struct tee_param param[2];
  612. if (rollback_index_slot >= TA_AVB_MAX_ROLLBACK_LOCATIONS)
  613. return AVB_IO_RESULT_ERROR_NO_SUCH_VALUE;
  614. memset(param, 0, sizeof(param));
  615. param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
  616. param[0].u.value.a = rollback_index_slot;
  617. param[1].attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT;
  618. rc = invoke_func(ops->user_data, TA_AVB_CMD_READ_ROLLBACK_INDEX,
  619. ARRAY_SIZE(param), param);
  620. if (rc)
  621. return rc;
  622. *out_rollback_index = (u64)param[1].u.value.a << 32 |
  623. (u32)param[1].u.value.b;
  624. return AVB_IO_RESULT_OK;
  625. #endif
  626. }
  627. /**
  628. * write_rollback_index() - sets the rollback index corresponding to the
  629. * location of given by @out_rollback_index.
  630. *
  631. * @ops: contains AvbOps handlers
  632. * @rollback_index_slot:
  633. * @rollback_index: rollback index to write.
  634. *
  635. * @return
  636. * AVB_IO_RESULT_OK, if the roolback index was retrieved
  637. */
  638. static AvbIOResult write_rollback_index(AvbOps *ops,
  639. size_t rollback_index_slot,
  640. u64 rollback_index)
  641. {
  642. #ifndef CONFIG_OPTEE_TA_AVB
  643. /* For now this is a no-op. */
  644. printf("%s not supported yet\n", __func__);
  645. return AVB_IO_RESULT_OK;
  646. #else
  647. struct tee_param param[2];
  648. if (rollback_index_slot >= TA_AVB_MAX_ROLLBACK_LOCATIONS)
  649. return AVB_IO_RESULT_ERROR_NO_SUCH_VALUE;
  650. memset(param, 0, sizeof(param));
  651. param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
  652. param[0].u.value.a = rollback_index_slot;
  653. param[1].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
  654. param[1].u.value.a = (u32)(rollback_index >> 32);
  655. param[1].u.value.b = (u32)rollback_index;
  656. return invoke_func(ops->user_data, TA_AVB_CMD_WRITE_ROLLBACK_INDEX,
  657. ARRAY_SIZE(param), param);
  658. #endif
  659. }
  660. /**
  661. * read_is_device_unlocked() - gets whether the device is unlocked
  662. *
  663. * @ops: contains AVB ops handlers
  664. * @out_is_unlocked: device unlock state is stored here, true if unlocked,
  665. * false otherwise
  666. *
  667. * @return:
  668. * AVB_IO_RESULT_OK: state is retrieved successfully
  669. * AVB_IO_RESULT_ERROR_IO: an error occurred
  670. */
  671. static AvbIOResult read_is_device_unlocked(AvbOps *ops, bool *out_is_unlocked)
  672. {
  673. #ifndef CONFIG_OPTEE_TA_AVB
  674. /* For now we always return that the device is unlocked. */
  675. printf("%s not supported yet\n", __func__);
  676. *out_is_unlocked = true;
  677. return AVB_IO_RESULT_OK;
  678. #else
  679. AvbIOResult rc;
  680. struct tee_param param = { .attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT };
  681. rc = invoke_func(ops->user_data, TA_AVB_CMD_READ_LOCK_STATE, 1, &param);
  682. if (rc)
  683. return rc;
  684. *out_is_unlocked = !param.u.value.a;
  685. return AVB_IO_RESULT_OK;
  686. #endif
  687. }
  688. /**
  689. * get_unique_guid_for_partition() - gets the GUID for a partition identified
  690. * by a string name
  691. *
  692. * @ops: contains AVB ops handlers
  693. * @partition: partition name (NUL-terminated UTF-8 string)
  694. * @guid_buf: buf, used to copy in GUID string. Example of value:
  695. * 527c1c6d-6361-4593-8842-3c78fcd39219
  696. * @guid_buf_size: @guid_buf buffer size
  697. *
  698. * @return:
  699. * AVB_IO_RESULT_OK, on success (GUID found)
  700. * AVB_IO_RESULT_ERROR_IO, if incorrect buffer size (@guid_buf_size) was
  701. * provided
  702. * AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION, if partition was not found
  703. */
  704. static AvbIOResult get_unique_guid_for_partition(AvbOps *ops,
  705. const char *partition,
  706. char *guid_buf,
  707. size_t guid_buf_size)
  708. {
  709. struct mmc_part *part;
  710. size_t uuid_size;
  711. part = get_partition(ops, partition);
  712. if (!part)
  713. return AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION;
  714. uuid_size = sizeof(part->info.uuid);
  715. if (uuid_size > guid_buf_size)
  716. return AVB_IO_RESULT_ERROR_IO;
  717. memcpy(guid_buf, part->info.uuid, uuid_size);
  718. guid_buf[uuid_size - 1] = 0;
  719. return AVB_IO_RESULT_OK;
  720. }
  721. /**
  722. * get_size_of_partition() - gets the size of a partition identified
  723. * by a string name
  724. *
  725. * @ops: contains AVB ops handlers
  726. * @partition: partition name (NUL-terminated UTF-8 string)
  727. * @out_size_num_bytes: returns the value of a partition size
  728. *
  729. * @return:
  730. * AVB_IO_RESULT_OK, on success (GUID found)
  731. * AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE, out_size_num_bytes is NULL
  732. * AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION, if partition was not found
  733. */
  734. static AvbIOResult get_size_of_partition(AvbOps *ops,
  735. const char *partition,
  736. u64 *out_size_num_bytes)
  737. {
  738. struct mmc_part *part;
  739. if (!out_size_num_bytes)
  740. return AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE;
  741. part = get_partition(ops, partition);
  742. if (!part)
  743. return AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION;
  744. *out_size_num_bytes = part->info.blksz * part->info.size;
  745. return AVB_IO_RESULT_OK;
  746. }
  747. #ifdef CONFIG_OPTEE_TA_AVB
  748. static AvbIOResult read_persistent_value(AvbOps *ops,
  749. const char *name,
  750. size_t buffer_size,
  751. u8 *out_buffer,
  752. size_t *out_num_bytes_read)
  753. {
  754. AvbIOResult rc;
  755. struct tee_shm *shm_name;
  756. struct tee_shm *shm_buf;
  757. struct tee_param param[2];
  758. struct udevice *tee;
  759. size_t name_size = strlen(name) + 1;
  760. if (get_open_session(ops->user_data))
  761. return AVB_IO_RESULT_ERROR_IO;
  762. tee = ((struct AvbOpsData *)ops->user_data)->tee;
  763. rc = tee_shm_alloc(tee, name_size,
  764. TEE_SHM_ALLOC, &shm_name);
  765. if (rc)
  766. return AVB_IO_RESULT_ERROR_OOM;
  767. rc = tee_shm_alloc(tee, buffer_size,
  768. TEE_SHM_ALLOC, &shm_buf);
  769. if (rc) {
  770. rc = AVB_IO_RESULT_ERROR_OOM;
  771. goto free_name;
  772. }
  773. memcpy(shm_name->addr, name, name_size);
  774. memset(param, 0, sizeof(param));
  775. param[0].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
  776. param[0].u.memref.shm = shm_name;
  777. param[0].u.memref.size = name_size;
  778. param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT;
  779. param[1].u.memref.shm = shm_buf;
  780. param[1].u.memref.size = buffer_size;
  781. rc = invoke_func(ops->user_data, TA_AVB_CMD_READ_PERSIST_VALUE,
  782. 2, param);
  783. if (rc)
  784. goto out;
  785. if (param[1].u.memref.size > buffer_size) {
  786. rc = AVB_IO_RESULT_ERROR_NO_SUCH_VALUE;
  787. goto out;
  788. }
  789. *out_num_bytes_read = param[1].u.memref.size;
  790. memcpy(out_buffer, shm_buf->addr, *out_num_bytes_read);
  791. out:
  792. tee_shm_free(shm_buf);
  793. free_name:
  794. tee_shm_free(shm_name);
  795. return rc;
  796. }
  797. static AvbIOResult write_persistent_value(AvbOps *ops,
  798. const char *name,
  799. size_t value_size,
  800. const u8 *value)
  801. {
  802. AvbIOResult rc;
  803. struct tee_shm *shm_name;
  804. struct tee_shm *shm_buf;
  805. struct tee_param param[2];
  806. struct udevice *tee;
  807. size_t name_size = strlen(name) + 1;
  808. if (get_open_session(ops->user_data))
  809. return AVB_IO_RESULT_ERROR_IO;
  810. tee = ((struct AvbOpsData *)ops->user_data)->tee;
  811. if (!value_size)
  812. return AVB_IO_RESULT_ERROR_NO_SUCH_VALUE;
  813. rc = tee_shm_alloc(tee, name_size,
  814. TEE_SHM_ALLOC, &shm_name);
  815. if (rc)
  816. return AVB_IO_RESULT_ERROR_OOM;
  817. rc = tee_shm_alloc(tee, value_size,
  818. TEE_SHM_ALLOC, &shm_buf);
  819. if (rc) {
  820. rc = AVB_IO_RESULT_ERROR_OOM;
  821. goto free_name;
  822. }
  823. memcpy(shm_name->addr, name, name_size);
  824. memcpy(shm_buf->addr, value, value_size);
  825. memset(param, 0, sizeof(param));
  826. param[0].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
  827. param[0].u.memref.shm = shm_name;
  828. param[0].u.memref.size = name_size;
  829. param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
  830. param[1].u.memref.shm = shm_buf;
  831. param[1].u.memref.size = value_size;
  832. rc = invoke_func(ops->user_data, TA_AVB_CMD_WRITE_PERSIST_VALUE,
  833. 2, param);
  834. if (rc)
  835. goto out;
  836. out:
  837. tee_shm_free(shm_buf);
  838. free_name:
  839. tee_shm_free(shm_name);
  840. return rc;
  841. }
  842. #endif
  843. /**
  844. * ============================================================================
  845. * AVB2.0 AvbOps alloc/initialisation/free
  846. * ============================================================================
  847. */
  848. AvbOps *avb_ops_alloc(int boot_device)
  849. {
  850. struct AvbOpsData *ops_data;
  851. ops_data = avb_calloc(sizeof(struct AvbOpsData));
  852. if (!ops_data)
  853. return NULL;
  854. ops_data->ops.user_data = ops_data;
  855. ops_data->ops.read_from_partition = read_from_partition;
  856. ops_data->ops.write_to_partition = write_to_partition;
  857. ops_data->ops.validate_vbmeta_public_key = validate_vbmeta_public_key;
  858. ops_data->ops.read_rollback_index = read_rollback_index;
  859. ops_data->ops.write_rollback_index = write_rollback_index;
  860. ops_data->ops.read_is_device_unlocked = read_is_device_unlocked;
  861. ops_data->ops.get_unique_guid_for_partition =
  862. get_unique_guid_for_partition;
  863. #ifdef CONFIG_OPTEE_TA_AVB
  864. ops_data->ops.write_persistent_value = write_persistent_value;
  865. ops_data->ops.read_persistent_value = read_persistent_value;
  866. #endif
  867. ops_data->ops.get_size_of_partition = get_size_of_partition;
  868. ops_data->mmc_dev = boot_device;
  869. return &ops_data->ops;
  870. }
  871. void avb_ops_free(AvbOps *ops)
  872. {
  873. struct AvbOpsData *ops_data;
  874. if (!ops)
  875. return;
  876. ops_data = ops->user_data;
  877. if (ops_data) {
  878. #ifdef CONFIG_OPTEE_TA_AVB
  879. if (ops_data->tee)
  880. tee_close_session(ops_data->tee, ops_data->session);
  881. #endif
  882. avb_free(ops_data);
  883. }
  884. }