avb_verify.c 23 KB

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