aisimage.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2011
  4. * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
  5. */
  6. #include "imagetool.h"
  7. #include "aisimage.h"
  8. #include <image.h>
  9. #define IS_FNC_EXEC(c) (cmd_table[c].AIS_cmd == AIS_CMD_FNLOAD)
  10. #define WORD_ALIGN0 4
  11. #define MAX_CMD_BUFFER 4096
  12. static uint32_t ais_img_size;
  13. /*
  14. * Supported commands for configuration file
  15. */
  16. static table_entry_t aisimage_cmds[] = {
  17. {CMD_DATA, "DATA", "Reg Write Data"},
  18. {CMD_FILL, "FILL", "Fill range with pattern"},
  19. {CMD_CRCON, "CRCON", "CRC Enable"},
  20. {CMD_CRCOFF, "CRCOFF", "CRC Disable"},
  21. {CMD_CRCCHECK, "CRCCHECK", "CRC Validate"},
  22. {CMD_JMPCLOSE, "JMPCLOSE", "Jump & Close"},
  23. {CMD_JMP, "JMP", "Jump"},
  24. {CMD_SEQREAD, "SEQREAD", "Sequential read"},
  25. {CMD_PLL0, "PLL0", "PLL0"},
  26. {CMD_PLL1, "PLL1", "PLL1"},
  27. {CMD_CLK, "CLK", "Clock configuration"},
  28. {CMD_DDR2, "DDR2", "DDR2 Configuration"},
  29. {CMD_EMIFA, "EMIFA", "EMIFA"},
  30. {CMD_EMIFA_ASYNC, "EMIFA_ASYNC", "EMIFA Async"},
  31. {CMD_PLL, "PLL", "PLL & Clock configuration"},
  32. {CMD_PSC, "PSC", "PSC setup"},
  33. {CMD_PINMUX, "PINMUX", "Pinmux setup"},
  34. {CMD_BOOTTABLE, "BOOT_TABLE", "Boot table command"},
  35. {-1, "", ""},
  36. };
  37. static struct ais_func_exec {
  38. uint32_t index;
  39. uint32_t argcnt;
  40. } ais_func_table[] = {
  41. [CMD_PLL0] = {0, 2},
  42. [CMD_PLL1] = {1, 2},
  43. [CMD_CLK] = {2, 1},
  44. [CMD_DDR2] = {3, 8},
  45. [CMD_EMIFA] = {4, 5},
  46. [CMD_EMIFA_ASYNC] = {5, 5},
  47. [CMD_PLL] = {6, 3},
  48. [CMD_PSC] = {7, 1},
  49. [CMD_PINMUX] = {8, 3}
  50. };
  51. static struct cmd_table_t {
  52. uint32_t nargs;
  53. uint32_t AIS_cmd;
  54. } cmd_table[] = {
  55. [CMD_FILL] = { 4, AIS_CMD_FILL},
  56. [CMD_CRCON] = { 0, AIS_CMD_ENCRC},
  57. [CMD_CRCOFF] = { 0, AIS_CMD_DISCRC},
  58. [CMD_CRCCHECK] = { 2, AIS_CMD_ENCRC},
  59. [CMD_JMPCLOSE] = { 1, AIS_CMD_JMPCLOSE},
  60. [CMD_JMP] = { 1, AIS_CMD_JMP},
  61. [CMD_SEQREAD] = { 0, AIS_CMD_SEQREAD},
  62. [CMD_PLL0] = { 2, AIS_CMD_FNLOAD},
  63. [CMD_PLL1] = { 2, AIS_CMD_FNLOAD},
  64. [CMD_CLK] = { 1, AIS_CMD_FNLOAD},
  65. [CMD_DDR2] = { 8, AIS_CMD_FNLOAD},
  66. [CMD_EMIFA] = { 5, AIS_CMD_FNLOAD},
  67. [CMD_EMIFA_ASYNC] = { 5, AIS_CMD_FNLOAD},
  68. [CMD_PLL] = { 3, AIS_CMD_FNLOAD},
  69. [CMD_PSC] = { 1, AIS_CMD_FNLOAD},
  70. [CMD_PINMUX] = { 3, AIS_CMD_FNLOAD},
  71. [CMD_BOOTTABLE] = { 4, AIS_CMD_BOOTTBL},
  72. };
  73. static uint32_t get_cfg_value(char *token, char *name, int linenr)
  74. {
  75. char *endptr;
  76. uint32_t value;
  77. errno = 0;
  78. value = strtoul(token, &endptr, 16);
  79. if (errno || (token == endptr)) {
  80. fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
  81. name, linenr, token);
  82. exit(EXIT_FAILURE);
  83. }
  84. return value;
  85. }
  86. static int get_ais_table_id(uint32_t *ptr)
  87. {
  88. int i;
  89. int func_no;
  90. for (i = 0; i < ARRAY_SIZE(cmd_table); i++) {
  91. if (*ptr == cmd_table[i].AIS_cmd) {
  92. if (cmd_table[i].AIS_cmd != AIS_CMD_FNLOAD)
  93. return i;
  94. func_no = ((struct ais_cmd_func *)ptr)->func_args
  95. & 0xFFFF;
  96. if (func_no == ais_func_table[i].index)
  97. return i;
  98. }
  99. }
  100. return -1;
  101. }
  102. static void aisimage_print_header(const void *hdr)
  103. {
  104. struct ais_header *ais_hdr = (struct ais_header *)hdr;
  105. uint32_t *ptr;
  106. struct ais_cmd_load *ais_load;
  107. int id;
  108. if (ais_hdr->magic != AIS_MAGIC_WORD) {
  109. fprintf(stderr, "Error: - AIS Magic Number not found\n");
  110. return;
  111. }
  112. fprintf(stdout, "Image Type: TI Davinci AIS Boot Image\n");
  113. fprintf(stdout, "AIS magic : %08x\n", ais_hdr->magic);
  114. ptr = (uint32_t *)&ais_hdr->magic;
  115. ptr++;
  116. while (*ptr != AIS_CMD_JMPCLOSE) {
  117. /* Check if we find the image */
  118. if (*ptr == AIS_CMD_LOAD) {
  119. ais_load = (struct ais_cmd_load *)ptr;
  120. fprintf(stdout, "Image at : 0x%08x size 0x%08x\n",
  121. ais_load->addr,
  122. ais_load->size);
  123. ptr = ais_load->data + ais_load->size / sizeof(*ptr);
  124. continue;
  125. }
  126. id = get_ais_table_id(ptr);
  127. if (id < 0) {
  128. fprintf(stderr, "Error: - AIS Image corrupted\n");
  129. return;
  130. }
  131. fprintf(stdout, "AIS cmd : %s\n",
  132. get_table_entry_name(aisimage_cmds, NULL, id));
  133. ptr += cmd_table[id].nargs + IS_FNC_EXEC(id) + 1;
  134. if (((void *)ptr - hdr) > ais_img_size) {
  135. fprintf(stderr,
  136. "AIS Image not terminated by JMPCLOSE\n");
  137. return;
  138. }
  139. }
  140. }
  141. static uint32_t *ais_insert_cmd_header(uint32_t cmd, uint32_t nargs,
  142. uint32_t *parms, struct image_type_params *tparams,
  143. uint32_t *ptr)
  144. {
  145. int i;
  146. *ptr++ = cmd_table[cmd].AIS_cmd;
  147. if (IS_FNC_EXEC(cmd))
  148. *ptr++ = ((nargs & 0xFFFF) << 16) + ais_func_table[cmd].index;
  149. /* Copy parameters */
  150. for (i = 0; i < nargs; i++)
  151. *ptr++ = cpu_to_le32(parms[i]);
  152. return ptr;
  153. }
  154. static uint32_t *ais_alloc_buffer(struct image_tool_params *params)
  155. {
  156. int dfd;
  157. struct stat sbuf;
  158. char *datafile = params->datafile;
  159. uint32_t *ptr;
  160. dfd = open(datafile, O_RDONLY|O_BINARY);
  161. if (dfd < 0) {
  162. fprintf(stderr, "%s: Can't open %s: %s\n",
  163. params->cmdname, datafile, strerror(errno));
  164. exit(EXIT_FAILURE);
  165. }
  166. if (fstat(dfd, &sbuf) < 0) {
  167. fprintf(stderr, "%s: Can't stat %s: %s\n",
  168. params->cmdname, datafile, strerror(errno));
  169. exit(EXIT_FAILURE);
  170. }
  171. /*
  172. * Place for header is allocated. The size is taken from
  173. * the size of the datafile, that the ais_image_generate()
  174. * will copy into the header. Copying the datafile
  175. * is not left to the main program, because after the datafile
  176. * the header must be terminated with the Jump & Close command.
  177. */
  178. ais_img_size = ALIGN(sbuf.st_size, WORD_ALIGN0) + MAX_CMD_BUFFER;
  179. ptr = (uint32_t *)malloc(ALIGN(sbuf.st_size, WORD_ALIGN0)
  180. + MAX_CMD_BUFFER);
  181. if (!ptr) {
  182. fprintf(stderr, "%s: malloc return failure: %s\n",
  183. params->cmdname, strerror(errno));
  184. exit(EXIT_FAILURE);
  185. }
  186. close(dfd);
  187. return ptr;
  188. }
  189. static uint32_t *ais_copy_image(struct image_tool_params *params,
  190. uint32_t *aisptr)
  191. {
  192. int dfd;
  193. struct stat sbuf;
  194. char *datafile = params->datafile;
  195. void *ptr;
  196. dfd = open(datafile, O_RDONLY|O_BINARY);
  197. if (dfd < 0) {
  198. fprintf(stderr, "%s: Can't open %s: %s\n",
  199. params->cmdname, datafile, strerror(errno));
  200. exit(EXIT_FAILURE);
  201. }
  202. if (fstat(dfd, &sbuf) < 0) {
  203. fprintf(stderr, "%s: Can't stat %s: %s\n",
  204. params->cmdname, datafile, strerror(errno));
  205. exit(EXIT_FAILURE);
  206. }
  207. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
  208. *aisptr++ = AIS_CMD_LOAD;
  209. *aisptr++ = params->ep;
  210. *aisptr++ = sbuf.st_size;
  211. memcpy((void *)aisptr, ptr, sbuf.st_size);
  212. aisptr += ALIGN(sbuf.st_size, WORD_ALIGN0) / sizeof(uint32_t);
  213. (void) munmap((void *)ptr, sbuf.st_size);
  214. (void) close(dfd);
  215. return aisptr;
  216. }
  217. static int aisimage_generate(struct image_tool_params *params,
  218. struct image_type_params *tparams)
  219. {
  220. FILE *fd = NULL;
  221. char *line = NULL;
  222. char *token, *saveptr1, *saveptr2;
  223. int lineno = 0;
  224. int fld;
  225. size_t len;
  226. int32_t cmd;
  227. uint32_t nargs, cmd_parms[10];
  228. uint32_t value, size;
  229. char *name = params->imagename;
  230. uint32_t *aishdr;
  231. fd = fopen(name, "r");
  232. if (fd == 0) {
  233. fprintf(stderr,
  234. "Error: %s - Can't open AIS configuration\n", name);
  235. exit(EXIT_FAILURE);
  236. }
  237. /*
  238. * the size of the header is variable and is computed
  239. * scanning the configuration file.
  240. */
  241. tparams->header_size = 0;
  242. /*
  243. * Start allocating a buffer suitable for most command
  244. * The buffer is then reallocated if it is too small
  245. */
  246. aishdr = ais_alloc_buffer(params);
  247. tparams->hdr = aishdr;
  248. *aishdr++ = AIS_MAGIC_WORD;
  249. /* Very simple parsing, line starting with # are comments
  250. * and are dropped
  251. */
  252. while ((getline(&line, &len, fd)) > 0) {
  253. lineno++;
  254. token = strtok_r(line, "\r\n", &saveptr1);
  255. if (token == NULL)
  256. continue;
  257. /* Check inside the single line */
  258. line = token;
  259. fld = CFG_COMMAND;
  260. cmd = CMD_INVALID;
  261. nargs = 0;
  262. while (token != NULL) {
  263. token = strtok_r(line, " \t", &saveptr2);
  264. if (token == NULL)
  265. break;
  266. /* Drop all text starting with '#' as comments */
  267. if (token[0] == '#')
  268. break;
  269. switch (fld) {
  270. case CFG_COMMAND:
  271. cmd = get_table_entry_id(aisimage_cmds,
  272. "aisimage commands", token);
  273. if (cmd < 0) {
  274. fprintf(stderr,
  275. "Error: %s[%d] - Invalid command"
  276. "(%s)\n", name, lineno, token);
  277. exit(EXIT_FAILURE);
  278. }
  279. break;
  280. case CFG_VALUE:
  281. value = get_cfg_value(token, name, lineno);
  282. cmd_parms[nargs++] = value;
  283. if (nargs > cmd_table[cmd].nargs) {
  284. fprintf(stderr,
  285. "Error: %s[%d] - too much arguments:"
  286. "(%s) for command %s\n", name,
  287. lineno, token,
  288. aisimage_cmds[cmd].sname);
  289. exit(EXIT_FAILURE);
  290. }
  291. break;
  292. }
  293. line = NULL;
  294. fld = CFG_VALUE;
  295. }
  296. if (cmd != CMD_INVALID) {
  297. /* Now insert the command into the header */
  298. aishdr = ais_insert_cmd_header(cmd, nargs, cmd_parms,
  299. tparams, aishdr);
  300. }
  301. }
  302. fclose(fd);
  303. aishdr = ais_copy_image(params, aishdr);
  304. /* Add Jmp & Close */
  305. *aishdr++ = AIS_CMD_JMPCLOSE;
  306. *aishdr++ = params->ep;
  307. size = (aishdr - (uint32_t *)tparams->hdr) * sizeof(uint32_t);
  308. tparams->header_size = size;
  309. return 0;
  310. }
  311. static int aisimage_check_image_types(uint8_t type)
  312. {
  313. if (type == IH_TYPE_AISIMAGE)
  314. return EXIT_SUCCESS;
  315. else
  316. return EXIT_FAILURE;
  317. }
  318. static int aisimage_verify_header(unsigned char *ptr, int image_size,
  319. struct image_tool_params *params)
  320. {
  321. struct ais_header *ais_hdr = (struct ais_header *)ptr;
  322. if (ais_hdr->magic != AIS_MAGIC_WORD)
  323. return -FDT_ERR_BADSTRUCTURE;
  324. /* Store the total size to remember in print_hdr */
  325. ais_img_size = image_size;
  326. return 0;
  327. }
  328. static void aisimage_set_header(void *ptr, struct stat *sbuf, int ifd,
  329. struct image_tool_params *params)
  330. {
  331. }
  332. int aisimage_check_params(struct image_tool_params *params)
  333. {
  334. if (!params)
  335. return CFG_INVALID;
  336. if (!strlen(params->imagename)) {
  337. fprintf(stderr, "Error: %s - Configuration file not specified, "
  338. "it is needed for aisimage generation\n",
  339. params->cmdname);
  340. return CFG_INVALID;
  341. }
  342. /*
  343. * Check parameters:
  344. * XIP is not allowed and verify that incompatible
  345. * parameters are not sent at the same time
  346. * For example, if list is required a data image must not be provided
  347. */
  348. return (params->dflag && (params->fflag || params->lflag)) ||
  349. (params->fflag && (params->dflag || params->lflag)) ||
  350. (params->lflag && (params->dflag || params->fflag)) ||
  351. (params->xflag) || !(strlen(params->imagename));
  352. }
  353. /*
  354. * aisimage parameters
  355. */
  356. U_BOOT_IMAGE_TYPE(
  357. aisimage,
  358. "TI Davinci AIS Boot Image support",
  359. 0,
  360. NULL,
  361. aisimage_check_params,
  362. aisimage_verify_header,
  363. aisimage_print_header,
  364. aisimage_set_header,
  365. NULL,
  366. aisimage_check_image_types,
  367. NULL,
  368. aisimage_generate
  369. );