aisimage.c 11 KB

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