avb_verify.c 29 KB

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