aisimage.c 10 KB

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