avb_verify.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /*
  2. * (C) Copyright 2018, Linaro Limited
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <avb_verify.h>
  7. #include <fastboot.h>
  8. #include <image.h>
  9. #include <malloc.h>
  10. #include <part.h>
  11. const unsigned char avb_root_pub[1032] = {
  12. 0x0, 0x0, 0x10, 0x0, 0x55, 0xd9, 0x4, 0xad, 0xd8, 0x4,
  13. 0xaf, 0xe3, 0xd3, 0x84, 0x6c, 0x7e, 0xd, 0x89, 0x3d, 0xc2,
  14. 0x8c, 0xd3, 0x12, 0x55, 0xe9, 0x62, 0xc9, 0xf1, 0xf, 0x5e,
  15. 0xcc, 0x16, 0x72, 0xab, 0x44, 0x7c, 0x2c, 0x65, 0x4a, 0x94,
  16. 0xb5, 0x16, 0x2b, 0x0, 0xbb, 0x6, 0xef, 0x13, 0x7, 0x53,
  17. 0x4c, 0xf9, 0x64, 0xb9, 0x28, 0x7a, 0x1b, 0x84, 0x98, 0x88,
  18. 0xd8, 0x67, 0xa4, 0x23, 0xf9, 0xa7, 0x4b, 0xdc, 0x4a, 0xf,
  19. 0xf7, 0x3a, 0x18, 0xae, 0x54, 0xa8, 0x15, 0xfe, 0xb0, 0xad,
  20. 0xac, 0x35, 0xda, 0x3b, 0xad, 0x27, 0xbc, 0xaf, 0xe8, 0xd3,
  21. 0x2f, 0x37, 0x34, 0xd6, 0x51, 0x2b, 0x6c, 0x5a, 0x27, 0xd7,
  22. 0x96, 0x6, 0xaf, 0x6b, 0xb8, 0x80, 0xca, 0xfa, 0x30, 0xb4,
  23. 0xb1, 0x85, 0xb3, 0x4d, 0xaa, 0xaa, 0xc3, 0x16, 0x34, 0x1a,
  24. 0xb8, 0xe7, 0xc7, 0xfa, 0xf9, 0x9, 0x77, 0xab, 0x97, 0x93,
  25. 0xeb, 0x44, 0xae, 0xcf, 0x20, 0xbc, 0xf0, 0x80, 0x11, 0xdb,
  26. 0x23, 0xc, 0x47, 0x71, 0xb9, 0x6d, 0xd6, 0x7b, 0x60, 0x47,
  27. 0x87, 0x16, 0x56, 0x93, 0xb7, 0xc2, 0x2a, 0x9a, 0xb0, 0x4c,
  28. 0x1, 0xc, 0x30, 0xd8, 0x93, 0x87, 0xf0, 0xed, 0x6e, 0x8b,
  29. 0xbe, 0x30, 0x5b, 0xf6, 0xa6, 0xaf, 0xdd, 0x80, 0x7c, 0x45,
  30. 0x5e, 0x8f, 0x91, 0x93, 0x5e, 0x44, 0xfe, 0xb8, 0x82, 0x7,
  31. 0xee, 0x79, 0xca, 0xbf, 0x31, 0x73, 0x62, 0x58, 0xe3, 0xc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49. 0x4d, 0x3f, 0xc1, 0xb5, 0xbb, 0x70, 0x5f, 0xa7, 0x37, 0xaf,
  50. 0x48, 0x22, 0x12, 0x45, 0x2e, 0xf5, 0xf, 0x87, 0x92, 0xe2,
  51. 0x84, 0x1, 0xf9, 0x12, 0xf, 0x14, 0x15, 0x24, 0xce, 0x89,
  52. 0x99, 0xee, 0xb9, 0xc4, 0x17, 0x70, 0x70, 0x15, 0xea, 0xbe,
  53. 0xc6, 0x6c, 0x1f, 0x62, 0xb3, 0xf4, 0x2d, 0x16, 0x87, 0xfb,
  54. 0x56, 0x1e, 0x45, 0xab, 0xae, 0x32, 0xe4, 0x5e, 0x91, 0xed,
  55. 0x53, 0x66, 0x5e, 0xbd, 0xed, 0xad, 0xe6, 0x12, 0x39, 0xd,
  56. 0x83, 0xc9, 0xe8, 0x6b, 0x6c, 0x2d, 0xa5, 0xee, 0xc4, 0x5a,
  57. 0x66, 0xae, 0x8c, 0x97, 0xd7, 0xd, 0x6c, 0x49, 0xc7, 0xf5,
  58. 0xc4, 0x92, 0x31, 0x8b, 0x9, 0xee, 0x33, 0xda, 0xa9, 0x37,
  59. 0xb6, 0x49, 0x18, 0xf8, 0xe, 0x60, 0x45, 0xc8, 0x33, 0x91,
  60. 0xef, 0x20, 0x57, 0x10, 0xbe, 0x78, 0x2d, 0x83, 0x26, 0xd6,
  61. 0xca, 0x61, 0xf9, 0x2f, 0xe0, 0xbf, 0x5, 0x30, 0x52, 0x5a,
  62. 0x12, 0x1c, 0x0, 0xa7, 0x5d, 0xcc, 0x7c, 0x2e, 0xc5, 0x95,
  63. 0x8b, 0xa3, 0x3b, 0xf0, 0x43, 0x2e, 0x5e, 0xdd, 0x0, 0xdb,
  64. 0xd, 0xb3, 0x37, 0x99, 0xa9, 0xcd, 0x9c, 0xb7, 0x43, 0xf7,
  65. 0x35, 0x44, 0x21, 0xc2, 0x82, 0x71, 0xab, 0x8d, 0xaa, 0xb4,
  66. 0x41, 0x11, 0xec, 0x1e, 0x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 0xb0, 0xef, 0x80, 0x15,
  84. 0xb6, 0xd4, 0x4e, 0xdf, 0x71, 0xac, 0xdb, 0x2a, 0x4, 0xd4,
  85. 0xb4, 0x21, 0xba, 0x65, 0x56, 0x57, 0xe8, 0xfa, 0x84, 0xa2,
  86. 0x7d, 0x13, 0xe, 0xaf, 0xd7, 0x9a, 0x58, 0x2a, 0xa3, 0x81,
  87. 0x84, 0x8d, 0x9, 0xa0, 0x6a, 0xc1, 0xbb, 0xd9, 0xf5, 0x86,
  88. 0xac, 0xbd, 0x75, 0x61, 0x9, 0xe6, 0x8c, 0x3d, 0x77, 0xb2,
  89. 0xed, 0x30, 0x20, 0xe4, 0x0, 0x1d, 0x97, 0xe8, 0xbf, 0xc7,
  90. 0x0, 0x1b, 0x21, 0xb1, 0x16, 0xe7, 0x41, 0x67, 0x2e, 0xec,
  91. 0x38, 0xbc, 0xe5, 0x1b, 0xb4, 0x6, 0x23, 0x31, 0x71, 0x1c,
  92. 0x49, 0xcd, 0x76, 0x4a, 0x76, 0x36, 0x8d, 0xa3, 0x89, 0x8b,
  93. 0x4a, 0x7a, 0xf4, 0x87, 0xc8, 0x15, 0xf, 0x37, 0x39, 0xf6,
  94. 0x6d, 0x80, 0x19, 0xef, 0x5c, 0xa8, 0x66, 0xce, 0x1b, 0x16,
  95. 0x79, 0x21, 0xdf, 0xd7, 0x31, 0x30, 0xc4, 0x21, 0xdd, 0x34,
  96. 0x5b, 0xd2, 0x1a, 0x2b, 0x3e, 0x5d, 0xf7, 0xea, 0xca, 0x5,
  97. 0x8e, 0xb7, 0xcb, 0x49, 0x2e, 0xa0, 0xe3, 0xf4, 0xa7, 0x48,
  98. 0x19, 0x10, 0x9c, 0x4, 0xa7, 0xf4, 0x28, 0x74, 0xc8, 0x6f,
  99. 0x63, 0x20, 0x2b, 0x46, 0x24, 0x26, 0x19, 0x1d, 0xd1, 0x2c,
  100. 0x31, 0x6d, 0x5a, 0x29, 0xa2, 0x6, 0xa6, 0xb2, 0x41, 0xcc,
  101. 0xa, 0x27, 0x96, 0x9, 0x96, 0xac, 0x47, 0x65, 0x78, 0x68,
  102. 0x51, 0x98, 0xd6, 0xd8, 0xa6, 0x2d, 0xa0, 0xcf, 0xec, 0xe2,
  103. 0x74, 0xf2, 0x82, 0xe3, 0x97, 0xd9, 0x7e, 0xd4, 0xf8, 0xb,
  104. 0x70, 0x43, 0x3d, 0xb1, 0x7b, 0x97, 0x80, 0xd6, 0xcb, 0xd7,
  105. 0x19, 0xbc, 0x63, 0xb, 0xfd, 0x4d, 0x88, 0xfe, 0x67, 0xac,
  106. 0xb8, 0xcc, 0x50, 0xb7, 0x68, 0xb3, 0x5b, 0xd6, 0x1e, 0x25,
  107. 0xfc, 0x5f, 0x3c, 0x8d, 0xb1, 0x33, 0x7c, 0xb3, 0x49, 0x1,
  108. 0x3f, 0x71, 0x55, 0xe, 0x51, 0xba, 0x61, 0x26, 0xfa, 0xea,
  109. 0xe5, 0xb5, 0xe8, 0xaa, 0xcf, 0xcd, 0x96, 0x9f, 0xd6, 0xc1,
  110. 0x5f, 0x53, 0x91, 0xad, 0x5, 0xde, 0x20, 0xe7, 0x51, 0xda,
  111. 0x5b, 0x95, 0x67, 0xed, 0xf4, 0xee, 0x42, 0x65, 0x70, 0x13,
  112. 0xb, 0x70, 0x14, 0x1c, 0xc9, 0xe0, 0x19, 0xca, 0x5f, 0xf5,
  113. 0x1d, 0x70, 0x4b, 0x6c, 0x6, 0x74, 0xec, 0xb5, 0x2e, 0x77,
  114. 0xe1, 0x74, 0xa1, 0xa3, 0x99, 0xa0, 0x85, 0x9e, 0xf1, 0xac,
  115. 0xd8, 0x7e,
  116. };
  117. /**
  118. * ============================================================================
  119. * Boot states support (GREEN, YELLOW, ORANGE, RED) and dm_verity
  120. * ============================================================================
  121. */
  122. char *avb_set_state(AvbOps *ops, enum avb_boot_state boot_state)
  123. {
  124. struct AvbOpsData *data;
  125. char *cmdline = NULL;
  126. if (!ops)
  127. return NULL;
  128. data = (struct AvbOpsData *)ops->user_data;
  129. if (!data)
  130. return NULL;
  131. data->boot_state = boot_state;
  132. switch (boot_state) {
  133. case AVB_GREEN:
  134. cmdline = "androidboot.verifiedbootstate=green";
  135. break;
  136. case AVB_YELLOW:
  137. cmdline = "androidboot.verifiedbootstate=yellow";
  138. break;
  139. case AVB_ORANGE:
  140. cmdline = "androidboot.verifiedbootstate=orange";
  141. case AVB_RED:
  142. break;
  143. }
  144. return cmdline;
  145. }
  146. char *append_cmd_line(char *cmdline_orig, char *cmdline_new)
  147. {
  148. char *cmd_line;
  149. if (!cmdline_new)
  150. return cmdline_orig;
  151. if (cmdline_orig)
  152. cmd_line = cmdline_orig;
  153. else
  154. cmd_line = " ";
  155. cmd_line = avb_strdupv(cmd_line, " ", cmdline_new, NULL);
  156. return cmd_line;
  157. }
  158. static int avb_find_dm_args(char **args, char *str)
  159. {
  160. int i;
  161. if (!str)
  162. return -1;
  163. for (i = 0; i < AVB_MAX_ARGS, args[i]; ++i) {
  164. if (strstr(args[i], str))
  165. return i;
  166. }
  167. return -1;
  168. }
  169. static char *avb_set_enforce_option(const char *cmdline, const char *option)
  170. {
  171. char *cmdarg[AVB_MAX_ARGS];
  172. char *newargs = NULL;
  173. int i = 0;
  174. int total_args;
  175. memset(cmdarg, 0, sizeof(cmdarg));
  176. cmdarg[i++] = strtok((char *)cmdline, " ");
  177. do {
  178. cmdarg[i] = strtok(NULL, " ");
  179. if (!cmdarg[i])
  180. break;
  181. if (++i >= AVB_MAX_ARGS) {
  182. printf("%s: Can't handle more then %d args\n",
  183. __func__, i);
  184. return NULL;
  185. }
  186. } while (true);
  187. total_args = i;
  188. i = avb_find_dm_args(&cmdarg[0], VERITY_TABLE_OPT_LOGGING);
  189. if (i >= 0) {
  190. cmdarg[i] = (char *)option;
  191. } else {
  192. i = avb_find_dm_args(&cmdarg[0], VERITY_TABLE_OPT_RESTART);
  193. if (i < 0) {
  194. printf("%s: No verity options found\n", __func__);
  195. return NULL;
  196. }
  197. cmdarg[i] = (char *)option;
  198. }
  199. for (i = 0; i <= total_args; i++)
  200. newargs = append_cmd_line(newargs, cmdarg[i]);
  201. return newargs;
  202. }
  203. char *avb_set_ignore_corruption(const char *cmdline)
  204. {
  205. char *newargs = NULL;
  206. newargs = avb_set_enforce_option(cmdline, VERITY_TABLE_OPT_LOGGING);
  207. if (newargs)
  208. newargs = append_cmd_line(newargs,
  209. "androidboot.veritymode=eio");
  210. return newargs;
  211. }
  212. char *avb_set_enforce_verity(const char *cmdline)
  213. {
  214. char *newargs;
  215. newargs = avb_set_enforce_option(cmdline, VERITY_TABLE_OPT_RESTART);
  216. if (newargs)
  217. newargs = append_cmd_line(newargs,
  218. "androidboot.veritymode=enforcing");
  219. return newargs;
  220. }
  221. /**
  222. * ============================================================================
  223. * IO(mmc) auxiliary functions
  224. * ============================================================================
  225. */
  226. static unsigned long mmc_read_and_flush(struct mmc_part *part,
  227. lbaint_t start,
  228. lbaint_t sectors,
  229. void *buffer)
  230. {
  231. unsigned long blks;
  232. void *tmp_buf;
  233. size_t buf_size;
  234. bool unaligned = is_buf_unaligned(buffer);
  235. if (start < part->info.start) {
  236. printf("%s: partition start out of bounds\n", __func__);
  237. return 0;
  238. }
  239. if ((start + sectors) > (part->info.start + part->info.size)) {
  240. sectors = part->info.start + part->info.size - start;
  241. printf("%s: read sector aligned to partition bounds (%ld)\n",
  242. __func__, sectors);
  243. }
  244. /*
  245. * Reading fails on unaligned buffers, so we have to
  246. * use aligned temporary buffer and then copy to destination
  247. */
  248. if (unaligned) {
  249. printf("Handling unaligned read buffer..\n");
  250. tmp_buf = get_sector_buf();
  251. buf_size = get_sector_buf_size();
  252. if (sectors > buf_size / part->info.blksz)
  253. sectors = buf_size / part->info.blksz;
  254. } else {
  255. tmp_buf = buffer;
  256. }
  257. blks = part->mmc->block_dev.block_read(part->mmc_blk,
  258. start, sectors, tmp_buf);
  259. /* flush cache after read */
  260. flush_cache((ulong)tmp_buf, sectors * part->info.blksz);
  261. if (unaligned)
  262. memcpy(buffer, tmp_buf, sectors * part->info.blksz);
  263. return blks;
  264. }
  265. static unsigned long mmc_write(struct mmc_part *part, lbaint_t start,
  266. lbaint_t sectors, void *buffer)
  267. {
  268. void *tmp_buf;
  269. size_t buf_size;
  270. bool unaligned = is_buf_unaligned(buffer);
  271. if (start < part->info.start) {
  272. printf("%s: partition start out of bounds\n", __func__);
  273. return 0;
  274. }
  275. if ((start + sectors) > (part->info.start + part->info.size)) {
  276. sectors = part->info.start + part->info.size - start;
  277. printf("%s: sector aligned to partition bounds (%ld)\n",
  278. __func__, sectors);
  279. }
  280. if (unaligned) {
  281. tmp_buf = get_sector_buf();
  282. buf_size = get_sector_buf_size();
  283. printf("Handling unaligned wrire buffer..\n");
  284. if (sectors > buf_size / part->info.blksz)
  285. sectors = buf_size / part->info.blksz;
  286. memcpy(tmp_buf, buffer, sectors * part->info.blksz);
  287. } else {
  288. tmp_buf = buffer;
  289. }
  290. return part->mmc->block_dev.block_write(part->mmc_blk,
  291. start, sectors, tmp_buf);
  292. }
  293. static struct mmc_part *get_partition(AvbOps *ops, const char *partition)
  294. {
  295. int ret;
  296. u8 dev_num;
  297. int part_num = 0;
  298. struct mmc_part *part;
  299. struct blk_desc *mmc_blk;
  300. part = malloc(sizeof(struct mmc_part));
  301. if (!part)
  302. return NULL;
  303. dev_num = get_boot_device(ops);
  304. part->mmc = find_mmc_device(dev_num);
  305. if (!part->mmc) {
  306. printf("No MMC device at slot %x\n", dev_num);
  307. return NULL;
  308. }
  309. if (mmc_init(part->mmc)) {
  310. printf("MMC initialization failed\n");
  311. return NULL;
  312. }
  313. ret = mmc_switch_part(part->mmc, part_num);
  314. if (ret)
  315. return NULL;
  316. mmc_blk = mmc_get_blk_desc(part->mmc);
  317. if (!mmc_blk) {
  318. printf("Error - failed to obtain block descriptor\n");
  319. return NULL;
  320. }
  321. ret = part_get_info_by_name(mmc_blk, partition, &part->info);
  322. if (!ret) {
  323. printf("Can't find partition '%s'\n", partition);
  324. return NULL;
  325. }
  326. part->dev_num = dev_num;
  327. part->mmc_blk = mmc_blk;
  328. return part;
  329. }
  330. static AvbIOResult mmc_byte_io(AvbOps *ops,
  331. const char *partition,
  332. s64 offset,
  333. size_t num_bytes,
  334. void *buffer,
  335. size_t *out_num_read,
  336. enum mmc_io_type io_type)
  337. {
  338. ulong ret;
  339. struct mmc_part *part;
  340. u64 start_offset, start_sector, sectors, residue;
  341. u8 *tmp_buf;
  342. size_t io_cnt = 0;
  343. if (!partition || !buffer || io_type > IO_WRITE)
  344. return AVB_IO_RESULT_ERROR_IO;
  345. part = get_partition(ops, partition);
  346. if (!part)
  347. return AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION;
  348. start_offset = calc_offset(part, offset);
  349. while (num_bytes) {
  350. start_sector = start_offset / part->info.blksz;
  351. sectors = num_bytes / part->info.blksz;
  352. /* handle non block-aligned reads */
  353. if (start_offset % part->info.blksz ||
  354. num_bytes < part->info.blksz) {
  355. tmp_buf = get_sector_buf();
  356. if (start_offset % part->info.blksz) {
  357. residue = part->info.blksz -
  358. (start_offset % part->info.blksz);
  359. if (residue > num_bytes)
  360. residue = num_bytes;
  361. } else {
  362. residue = num_bytes;
  363. }
  364. if (io_type == IO_READ) {
  365. ret = mmc_read_and_flush(part,
  366. part->info.start +
  367. start_sector,
  368. 1, tmp_buf);
  369. if (ret != 1) {
  370. printf("%s: read error (%ld, %lld)\n",
  371. __func__, ret, start_sector);
  372. return AVB_IO_RESULT_ERROR_IO;
  373. }
  374. /*
  375. * if this is not aligned at sector start,
  376. * we have to adjust the tmp buffer
  377. */
  378. tmp_buf += (start_offset % part->info.blksz);
  379. memcpy(buffer, (void *)tmp_buf, residue);
  380. } else {
  381. ret = mmc_read_and_flush(part,
  382. part->info.start +
  383. start_sector,
  384. 1, tmp_buf);
  385. if (ret != 1) {
  386. printf("%s: read error (%ld, %lld)\n",
  387. __func__, ret, start_sector);
  388. return AVB_IO_RESULT_ERROR_IO;
  389. }
  390. memcpy((void *)tmp_buf +
  391. start_offset % part->info.blksz,
  392. buffer, residue);
  393. ret = mmc_write(part, part->info.start +
  394. start_sector, 1, tmp_buf);
  395. if (ret != 1) {
  396. printf("%s: write error (%ld, %lld)\n",
  397. __func__, ret, start_sector);
  398. return AVB_IO_RESULT_ERROR_IO;
  399. }
  400. }
  401. io_cnt += residue;
  402. buffer += residue;
  403. start_offset += residue;
  404. num_bytes -= residue;
  405. continue;
  406. }
  407. if (sectors) {
  408. if (io_type == IO_READ) {
  409. ret = mmc_read_and_flush(part,
  410. part->info.start +
  411. start_sector,
  412. sectors, buffer);
  413. } else {
  414. ret = mmc_write(part,
  415. part->info.start +
  416. start_sector,
  417. sectors, buffer);
  418. }
  419. if (!ret) {
  420. printf("%s: sector read error\n", __func__);
  421. return AVB_IO_RESULT_ERROR_IO;
  422. }
  423. io_cnt += ret * part->info.blksz;
  424. buffer += ret * part->info.blksz;
  425. start_offset += ret * part->info.blksz;
  426. num_bytes -= ret * part->info.blksz;
  427. }
  428. }
  429. /* Set counter for read operation */
  430. if (io_type == IO_READ && out_num_read)
  431. *out_num_read = io_cnt;
  432. return AVB_IO_RESULT_OK;
  433. }
  434. /**
  435. * ============================================================================
  436. * AVB 2.0 operations
  437. * ============================================================================
  438. */
  439. /**
  440. * read_from_partition() - reads @num_bytes from @offset from partition
  441. * identified by a string name
  442. *
  443. * @ops: contains AVB ops handlers
  444. * @partition_name: partition name, NUL-terminated UTF-8 string
  445. * @offset: offset from the beginning of partition
  446. * @num_bytes: amount of bytes to read
  447. * @buffer: destination buffer to store data
  448. * @out_num_read:
  449. *
  450. * @return:
  451. * AVB_IO_RESULT_OK, if partition was found and read operation succeed
  452. * AVB_IO_RESULT_ERROR_IO, if i/o error occurred from the underlying i/o
  453. * subsystem
  454. * AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION, if there is no partition with
  455. * the given name
  456. */
  457. static AvbIOResult read_from_partition(AvbOps *ops,
  458. const char *partition_name,
  459. s64 offset_from_partition,
  460. size_t num_bytes,
  461. void *buffer,
  462. size_t *out_num_read)
  463. {
  464. return mmc_byte_io(ops, partition_name, offset_from_partition,
  465. num_bytes, buffer, out_num_read, IO_READ);
  466. }
  467. /**
  468. * write_to_partition() - writes N bytes to a partition identified by a string
  469. * name
  470. *
  471. * @ops: AvbOps, contains AVB ops handlers
  472. * @partition_name: partition name
  473. * @offset_from_partition: offset from the beginning of partition
  474. * @num_bytes: amount of bytes to write
  475. * @buf: data to write
  476. * @out_num_read:
  477. *
  478. * @return:
  479. * AVB_IO_RESULT_OK, if partition was found and read operation succeed
  480. * AVB_IO_RESULT_ERROR_IO, if input/output error occurred
  481. * AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION, if partition, specified in
  482. * @partition_name was not found
  483. */
  484. static AvbIOResult write_to_partition(AvbOps *ops,
  485. const char *partition_name,
  486. s64 offset_from_partition,
  487. size_t num_bytes,
  488. const void *buffer)
  489. {
  490. return mmc_byte_io(ops, partition_name, offset_from_partition,
  491. num_bytes, (void *)buffer, NULL, IO_WRITE);
  492. }
  493. /**
  494. * validate_vmbeta_public_key() - checks if the given public key used to sign
  495. * the vbmeta partition is trusted
  496. *
  497. * @ops: AvbOps, contains AVB ops handlers
  498. * @public_key_data: public key for verifying vbmeta partition signature
  499. * @public_key_length: length of public key
  500. * @public_key_metadata:
  501. * @public_key_metadata_length:
  502. * @out_key_is_trusted:
  503. *
  504. * @return:
  505. * AVB_IO_RESULT_OK, if partition was found and read operation succeed
  506. */
  507. static AvbIOResult validate_vbmeta_public_key(AvbOps *ops,
  508. const u8 *public_key_data,
  509. size_t public_key_length,
  510. const u8
  511. *public_key_metadata,
  512. size_t
  513. public_key_metadata_length,
  514. bool *out_key_is_trusted)
  515. {
  516. if (!public_key_length || !public_key_data || !out_key_is_trusted)
  517. return AVB_IO_RESULT_ERROR_IO;
  518. *out_key_is_trusted = false;
  519. if (public_key_length != sizeof(avb_root_pub))
  520. return AVB_IO_RESULT_ERROR_IO;
  521. if (memcmp(avb_root_pub, public_key_data, public_key_length) == 0)
  522. *out_key_is_trusted = true;
  523. return AVB_IO_RESULT_OK;
  524. }
  525. /**
  526. * read_rollback_index() - gets the rollback index corresponding to the
  527. * location of given by @out_rollback_index.
  528. *
  529. * @ops: contains AvbOps handlers
  530. * @rollback_index_slot:
  531. * @out_rollback_index: used to write a retrieved rollback index.
  532. *
  533. * @return
  534. * AVB_IO_RESULT_OK, if the roolback index was retrieved
  535. */
  536. static AvbIOResult read_rollback_index(AvbOps *ops,
  537. size_t rollback_index_slot,
  538. u64 *out_rollback_index)
  539. {
  540. /* For now we always return 0 as the stored rollback index. */
  541. printf("%s not supported yet\n", __func__);
  542. if (out_rollback_index)
  543. *out_rollback_index = 0;
  544. return AVB_IO_RESULT_OK;
  545. }
  546. /**
  547. * write_rollback_index() - sets the rollback index corresponding to the
  548. * location of given by @out_rollback_index.
  549. *
  550. * @ops: contains AvbOps handlers
  551. * @rollback_index_slot:
  552. * @rollback_index: rollback index to write.
  553. *
  554. * @return
  555. * AVB_IO_RESULT_OK, if the roolback index was retrieved
  556. */
  557. static AvbIOResult write_rollback_index(AvbOps *ops,
  558. size_t rollback_index_slot,
  559. u64 rollback_index)
  560. {
  561. /* For now this is a no-op. */
  562. printf("%s not supported yet\n", __func__);
  563. return AVB_IO_RESULT_OK;
  564. }
  565. /**
  566. * read_is_device_unlocked() - gets whether the device is unlocked
  567. *
  568. * @ops: contains AVB ops handlers
  569. * @out_is_unlocked: device unlock state is stored here, true if unlocked,
  570. * false otherwise
  571. *
  572. * @return:
  573. * AVB_IO_RESULT_OK: state is retrieved successfully
  574. * AVB_IO_RESULT_ERROR_IO: an error occurred
  575. */
  576. static AvbIOResult read_is_device_unlocked(AvbOps *ops, bool *out_is_unlocked)
  577. {
  578. /* For now we always return that the device is unlocked. */
  579. printf("%s not supported yet\n", __func__);
  580. *out_is_unlocked = true;
  581. return AVB_IO_RESULT_OK;
  582. }
  583. /**
  584. * get_unique_guid_for_partition() - gets the GUID for a partition identified
  585. * by a string name
  586. *
  587. * @ops: contains AVB ops handlers
  588. * @partition: partition name (NUL-terminated UTF-8 string)
  589. * @guid_buf: buf, used to copy in GUID string. Example of value:
  590. * 527c1c6d-6361-4593-8842-3c78fcd39219
  591. * @guid_buf_size: @guid_buf buffer size
  592. *
  593. * @return:
  594. * AVB_IO_RESULT_OK, on success (GUID found)
  595. * AVB_IO_RESULT_ERROR_IO, if incorrect buffer size (@guid_buf_size) was
  596. * provided
  597. * AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION, if partition was not found
  598. */
  599. static AvbIOResult get_unique_guid_for_partition(AvbOps *ops,
  600. const char *partition,
  601. char *guid_buf,
  602. size_t guid_buf_size)
  603. {
  604. struct mmc_part *part;
  605. size_t uuid_size;
  606. part = get_partition(ops, partition);
  607. if (!part)
  608. return AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION;
  609. uuid_size = sizeof(part->info.uuid);
  610. if (uuid_size > guid_buf_size)
  611. return AVB_IO_RESULT_ERROR_IO;
  612. memcpy(guid_buf, part->info.uuid, uuid_size);
  613. guid_buf[uuid_size - 1] = 0;
  614. return AVB_IO_RESULT_OK;
  615. }
  616. /**
  617. * ============================================================================
  618. * AVB2.0 AvbOps alloc/initialisation/free
  619. * ============================================================================
  620. */
  621. AvbOps *avb_ops_alloc(int boot_device)
  622. {
  623. struct AvbOpsData *ops_data;
  624. ops_data = avb_calloc(sizeof(struct AvbOpsData));
  625. if (!ops_data)
  626. return NULL;
  627. ops_data->ops.user_data = ops_data;
  628. ops_data->ops.read_from_partition = read_from_partition;
  629. ops_data->ops.write_to_partition = write_to_partition;
  630. ops_data->ops.validate_vbmeta_public_key = validate_vbmeta_public_key;
  631. ops_data->ops.read_rollback_index = read_rollback_index;
  632. ops_data->ops.write_rollback_index = write_rollback_index;
  633. ops_data->ops.read_is_device_unlocked = read_is_device_unlocked;
  634. ops_data->ops.get_unique_guid_for_partition =
  635. get_unique_guid_for_partition;
  636. ops_data->mmc_dev = boot_device;
  637. return &ops_data->ops;
  638. }
  639. void avb_ops_free(AvbOps *ops)
  640. {
  641. struct AvbOpsData *ops_data;
  642. if (ops)
  643. return;
  644. ops_data = ops->user_data;
  645. if (ops_data)
  646. avb_free(ops_data);
  647. }