mkfwumdata.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2023, Linaro Limited
  4. */
  5. #include <errno.h>
  6. #include <getopt.h>
  7. #include <limits.h>
  8. #include <stdio.h>
  9. #include <stdint.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <u-boot/crc.h>
  13. #include <unistd.h>
  14. #include <uuid/uuid.h>
  15. /* This will dynamically allocate the fwu_mdata */
  16. #define CONFIG_FWU_NUM_BANKS 0
  17. #define CONFIG_FWU_NUM_IMAGES_PER_BANK 0
  18. /* Since we can not include fwu.h, redefine version here. */
  19. #define FWU_MDATA_VERSION 1
  20. typedef uint8_t u8;
  21. typedef int16_t s16;
  22. typedef uint16_t u16;
  23. typedef uint32_t u32;
  24. typedef uint64_t u64;
  25. #include <fwu_mdata.h>
  26. /* TODO: Endianness conversion may be required for some arch. */
  27. static const char *opts_short = "b:i:a:p:gh";
  28. static struct option options[] = {
  29. {"banks", required_argument, NULL, 'b'},
  30. {"images", required_argument, NULL, 'i'},
  31. {"guid", required_argument, NULL, 'g'},
  32. {"active-bank", required_argument, NULL, 'a'},
  33. {"previous-bank", required_argument, NULL, 'p'},
  34. {"help", no_argument, NULL, 'h'},
  35. {NULL, 0, NULL, 0},
  36. };
  37. static void print_usage(void)
  38. {
  39. fprintf(stderr, "Usage: mkfwumdata [options] <UUIDs list> <output file>\n");
  40. fprintf(stderr, "Options:\n"
  41. "\t-i, --images <num> Number of images (mandatory)\n"
  42. "\t-b, --banks <num> Number of banks (mandatory)\n"
  43. "\t-a, --active-bank <num> Active bank (default=0)\n"
  44. "\t-p, --previous-bank <num> Previous active bank (default=active_bank - 1)\n"
  45. "\t-g, --guid Use GUID instead of UUID\n"
  46. "\t-h, --help print a help message\n"
  47. );
  48. fprintf(stderr, " UUIDs list syntax:\n"
  49. "\t <location uuid>,<image type uuid>,<images uuid list>\n"
  50. "\t images uuid list syntax:\n"
  51. "\t img_uuid_00,img_uuid_01...img_uuid_0b,\n"
  52. "\t img_uuid_10,img_uuid_11...img_uuid_1b,\n"
  53. "\t ...,\n"
  54. "\t img_uuid_i0,img_uuid_i1...img_uuid_ib,\n"
  55. "\t where 'b' and 'i' are number of banks and number\n"
  56. "\t of images in a bank respectively.\n"
  57. );
  58. }
  59. struct fwu_mdata_object {
  60. size_t images;
  61. size_t banks;
  62. size_t size;
  63. struct fwu_mdata *mdata;
  64. };
  65. static int previous_bank, active_bank;
  66. static bool __use_guid;
  67. static struct fwu_mdata_object *fwu_alloc_mdata(size_t images, size_t banks)
  68. {
  69. struct fwu_mdata_object *mobj;
  70. mobj = calloc(1, sizeof(*mobj));
  71. if (!mobj)
  72. return NULL;
  73. mobj->size = sizeof(struct fwu_mdata) +
  74. (sizeof(struct fwu_image_entry) +
  75. sizeof(struct fwu_image_bank_info) * banks) * images;
  76. mobj->images = images;
  77. mobj->banks = banks;
  78. mobj->mdata = calloc(1, mobj->size);
  79. if (!mobj->mdata) {
  80. free(mobj);
  81. return NULL;
  82. }
  83. return mobj;
  84. }
  85. static struct fwu_image_entry *
  86. fwu_get_image(struct fwu_mdata_object *mobj, size_t idx)
  87. {
  88. size_t offset;
  89. offset = sizeof(struct fwu_mdata) +
  90. (sizeof(struct fwu_image_entry) +
  91. sizeof(struct fwu_image_bank_info) * mobj->banks) * idx;
  92. return (struct fwu_image_entry *)((char *)mobj->mdata + offset);
  93. }
  94. static struct fwu_image_bank_info *
  95. fwu_get_bank(struct fwu_mdata_object *mobj, size_t img_idx, size_t bnk_idx)
  96. {
  97. size_t offset;
  98. offset = sizeof(struct fwu_mdata) +
  99. (sizeof(struct fwu_image_entry) +
  100. sizeof(struct fwu_image_bank_info) * mobj->banks) * img_idx +
  101. sizeof(struct fwu_image_entry) +
  102. sizeof(struct fwu_image_bank_info) * bnk_idx;
  103. return (struct fwu_image_bank_info *)((char *)mobj->mdata + offset);
  104. }
  105. /**
  106. * convert_uuid_to_guid() - convert UUID to GUID
  107. * @buf: UUID binary
  108. *
  109. * UUID and GUID have the same data structure, but their binary
  110. * formats are different due to the endianness. See lib/uuid.c.
  111. * Since uuid_parse() can handle only UUID, this function must
  112. * be called to get correct data for GUID when parsing a string.
  113. *
  114. * The correct data will be returned in @buf.
  115. */
  116. static void convert_uuid_to_guid(unsigned char *buf)
  117. {
  118. unsigned char c;
  119. c = buf[0];
  120. buf[0] = buf[3];
  121. buf[3] = c;
  122. c = buf[1];
  123. buf[1] = buf[2];
  124. buf[2] = c;
  125. c = buf[4];
  126. buf[4] = buf[5];
  127. buf[5] = c;
  128. c = buf[6];
  129. buf[6] = buf[7];
  130. buf[7] = c;
  131. }
  132. static int uuid_guid_parse(char *uuidstr, unsigned char *uuid)
  133. {
  134. int ret;
  135. ret = uuid_parse(uuidstr, uuid);
  136. if (ret < 0)
  137. return ret;
  138. if (__use_guid)
  139. convert_uuid_to_guid(uuid);
  140. return ret;
  141. }
  142. static int
  143. fwu_parse_fill_image_uuid(struct fwu_mdata_object *mobj,
  144. size_t idx, char *uuids)
  145. {
  146. struct fwu_image_entry *image = fwu_get_image(mobj, idx);
  147. struct fwu_image_bank_info *bank;
  148. char *p = uuids, *uuid;
  149. int i;
  150. if (!image)
  151. return -ENOENT;
  152. /* Image location UUID */
  153. uuid = strsep(&p, ",");
  154. if (!uuid)
  155. return -EINVAL;
  156. if (strcmp(uuid, "0") &&
  157. uuid_guid_parse(uuid, (unsigned char *)&image->location_uuid) < 0)
  158. return -EINVAL;
  159. /* Image type UUID */
  160. uuid = strsep(&p, ",");
  161. if (!uuid)
  162. return -EINVAL;
  163. if (uuid_guid_parse(uuid, (unsigned char *)&image->image_type_uuid) < 0)
  164. return -EINVAL;
  165. /* Fill bank image-UUID */
  166. for (i = 0; i < mobj->banks; i++) {
  167. bank = fwu_get_bank(mobj, idx, i);
  168. if (!bank)
  169. return -ENOENT;
  170. bank->accepted = 1;
  171. uuid = strsep(&p, ",");
  172. if (!uuid)
  173. return -EINVAL;
  174. if (strcmp(uuid, "0") &&
  175. uuid_guid_parse(uuid, (unsigned char *)&bank->image_uuid) < 0)
  176. return -EINVAL;
  177. }
  178. return 0;
  179. }
  180. /* Caller must ensure that @uuids[] has @mobj->images entries. */
  181. static int fwu_parse_fill_uuids(struct fwu_mdata_object *mobj, char *uuids[])
  182. {
  183. struct fwu_mdata *mdata = mobj->mdata;
  184. int i, ret;
  185. mdata->version = FWU_MDATA_VERSION;
  186. mdata->active_index = active_bank;
  187. mdata->previous_active_index = previous_bank;
  188. for (i = 0; i < mobj->images; i++) {
  189. ret = fwu_parse_fill_image_uuid(mobj, i, uuids[i]);
  190. if (ret < 0)
  191. return ret;
  192. }
  193. mdata->crc32 = crc32(0, (const unsigned char *)&mdata->version,
  194. mobj->size - sizeof(uint32_t));
  195. return 0;
  196. }
  197. static int
  198. fwu_make_mdata(size_t images, size_t banks, char *uuids[], char *output)
  199. {
  200. struct fwu_mdata_object *mobj;
  201. FILE *file;
  202. int ret;
  203. mobj = fwu_alloc_mdata(images, banks);
  204. if (!mobj)
  205. return -ENOMEM;
  206. ret = fwu_parse_fill_uuids(mobj, uuids);
  207. if (ret < 0)
  208. goto done_make;
  209. file = fopen(output, "w");
  210. if (!file) {
  211. ret = -errno;
  212. goto done_make;
  213. }
  214. ret = fwrite(mobj->mdata, mobj->size, 1, file);
  215. if (ret != mobj->size)
  216. ret = -errno;
  217. else
  218. ret = 0;
  219. fclose(file);
  220. done_make:
  221. free(mobj->mdata);
  222. free(mobj);
  223. return ret;
  224. }
  225. int main(int argc, char *argv[])
  226. {
  227. unsigned long banks = 0, images = 0;
  228. int c, ret;
  229. /* Explicitly initialize defaults */
  230. active_bank = 0;
  231. __use_guid = false;
  232. previous_bank = INT_MAX;
  233. do {
  234. c = getopt_long(argc, argv, opts_short, options, NULL);
  235. switch (c) {
  236. case 'h':
  237. print_usage();
  238. return 0;
  239. case 'b':
  240. banks = strtoul(optarg, NULL, 0);
  241. break;
  242. case 'i':
  243. images = strtoul(optarg, NULL, 0);
  244. break;
  245. case 'g':
  246. __use_guid = true;
  247. break;
  248. case 'p':
  249. previous_bank = strtoul(optarg, NULL, 0);
  250. break;
  251. case 'a':
  252. active_bank = strtoul(optarg, NULL, 0);
  253. break;
  254. }
  255. } while (c != -1);
  256. if (!banks || !images) {
  257. fprintf(stderr, "Error: The number of banks and images must not be 0.\n");
  258. return -EINVAL;
  259. }
  260. /* This command takes UUIDs * images and output file. */
  261. if (optind + images + 1 != argc) {
  262. fprintf(stderr, "Error: UUID list or output file is not specified or too much.\n");
  263. print_usage();
  264. return -ERANGE;
  265. }
  266. if (previous_bank == INT_MAX) {
  267. /* set to the earlier bank in round-robin scheme */
  268. previous_bank = active_bank > 0 ? active_bank - 1 : banks - 1;
  269. }
  270. ret = fwu_make_mdata(images, banks, argv + optind, argv[argc - 1]);
  271. if (ret < 0)
  272. fprintf(stderr, "Error: Failed to parse and write image: %s\n",
  273. strerror(-ret));
  274. return ret;
  275. }