aisimage.c 11 KB

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