mkspk.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /****************************************************************************
  2. * tools/cxd56/mkspk.c
  3. *
  4. * Copyright (C) 2007, 2008 Sony Corporation
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. * 3. Neither the name NuttX nor the names of its contributors may be
  17. * used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. ****************************************************************************/
  33. /****************************************************************************
  34. * Included Files
  35. ****************************************************************************/
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <unistd.h>
  42. #include <stdint.h>
  43. #include <stdbool.h>
  44. #include <assert.h>
  45. #include "mkspk.h"
  46. /****************************************************************************
  47. * Private Types
  48. ****************************************************************************/
  49. struct args
  50. {
  51. int core;
  52. char *elffile;
  53. char *savename;
  54. char *outputfile;
  55. };
  56. /****************************************************************************
  57. * Private Data
  58. ****************************************************************************/
  59. static uint8_t vmk[16] =
  60. "\x27\xc0\xaf\x1b\x5d\xcb\xc6\xc5\x58\x22\x1c\xdd\xaf\xf3\x20\x21";
  61. static struct args g_options =
  62. {
  63. 0
  64. };
  65. /****************************************************************************
  66. * Private Functions
  67. ****************************************************************************/
  68. static struct args *parse_args(int argc, char **argv)
  69. {
  70. int opt;
  71. int show_help;
  72. struct args *args = &g_options;
  73. char *endp;
  74. show_help = 0;
  75. if (argc < 2)
  76. {
  77. show_help = 1;
  78. }
  79. memset(args, 0, sizeof(*args));
  80. args->core = -1;
  81. while ((opt = getopt(argc, argv, "h:c:")) != -1)
  82. {
  83. switch (opt)
  84. {
  85. case 'c':
  86. args->core = strtol(optarg, &endp, 0);
  87. if (*endp)
  88. {
  89. fprintf(stderr, "Invalid core number \"%s\"\n", optarg);
  90. show_help = 1;
  91. }
  92. break;
  93. case 'h':
  94. default:
  95. show_help = 1;
  96. }
  97. }
  98. argc -= optind;
  99. argv += optind;
  100. args->elffile = argv[0];
  101. args->savename = argv[1];
  102. argc -= 2;
  103. argv += 2;
  104. if (argc > 0)
  105. {
  106. args->outputfile = strdup(argv[0]);
  107. }
  108. else
  109. {
  110. show_help = 1;
  111. }
  112. /* Sanity checks for options */
  113. if (show_help == 1)
  114. {
  115. fprintf(stderr,
  116. "mkspk [-c <number>] <filename> <save name> [<output file>]\n");
  117. exit(EXIT_FAILURE);
  118. }
  119. if (args->core < 0)
  120. {
  121. fprintf(stderr, "Core number is not set. Please use -c option.\n");
  122. exit(EXIT_FAILURE);
  123. }
  124. if (strlen(args->savename) > 63)
  125. {
  126. fprintf(stderr, "savename too long.\n");
  127. exit(EXIT_FAILURE);
  128. }
  129. return args;
  130. }
  131. static struct elf_file *load_elf(const char *filename)
  132. {
  133. size_t fsize;
  134. int pos;
  135. char *buf;
  136. FILE *fp;
  137. struct elf_file *ef;
  138. Elf32_Shdr *sh;
  139. uint16_t i;
  140. int ret;
  141. fp = fopen(filename, "rb");
  142. if (!fp)
  143. {
  144. return NULL;
  145. }
  146. ef = (struct elf_file *)malloc(sizeof(*ef));
  147. if (!ef)
  148. {
  149. return NULL;
  150. }
  151. pos = fseek(fp, 0, SEEK_END);
  152. fsize = (size_t) ftell(fp);
  153. fseek(fp, pos, SEEK_SET);
  154. buf = (char *)malloc(fsize);
  155. if (!buf)
  156. {
  157. return NULL;
  158. }
  159. ret = fread(buf, fsize, 1, fp);
  160. fclose(fp);
  161. if (ret != 1)
  162. {
  163. return NULL;
  164. }
  165. ef->data = buf;
  166. ef->ehdr = (Elf32_Ehdr *) buf;
  167. Elf32_Ehdr *h = (Elf32_Ehdr *) buf;
  168. if (!(h->e_ident[EI_MAG0] == 0x7f &&
  169. h->e_ident[EI_MAG1] == 'E' &&
  170. h->e_ident[EI_MAG2] == 'L' && h->e_ident[EI_MAG3] == 'F'))
  171. {
  172. free(ef);
  173. free(buf);
  174. return NULL;
  175. }
  176. ef->phdr = (Elf32_Phdr *) (buf + ef->ehdr->e_phoff);
  177. ef->shdr = (Elf32_Shdr *) (buf + ef->ehdr->e_shoff);
  178. ef->shstring = buf + ef->shdr[ef->ehdr->e_shstrndx].sh_offset;
  179. for (i = 0, sh = ef->shdr; i < ef->ehdr->e_shnum; i++, sh++)
  180. {
  181. if (sh->sh_type == SHT_SYMTAB)
  182. {
  183. ef->symtab = (Elf32_Sym *) (buf + sh->sh_offset);
  184. ef->nsyms = sh->sh_size / sh->sh_entsize;
  185. continue;
  186. }
  187. if (sh->sh_type == SHT_STRTAB)
  188. {
  189. if (!strcmp(".strtab", ef->shstring + sh->sh_name))
  190. {
  191. ef->string = buf + sh->sh_offset;
  192. }
  193. }
  194. }
  195. return ef;
  196. }
  197. static void *create_image(struct elf_file *elf, int core, char *savename,
  198. int *image_size)
  199. {
  200. char *img;
  201. struct spk_header *header;
  202. struct spk_prog_info *pi;
  203. Elf32_Phdr *ph;
  204. Elf32_Sym *sym;
  205. char *name;
  206. int snlen;
  207. int nphs, psize, imgsize;
  208. int i;
  209. int j;
  210. uint32_t offset;
  211. uint32_t sp;
  212. snlen = alignup(strlen(savename) + 1, 16);
  213. nphs = 0;
  214. psize = 0;
  215. for (i = 0, ph = elf->phdr; i < elf->ehdr->e_phnum; i++, ph++)
  216. {
  217. if (ph->p_type != PT_LOAD || ph->p_filesz == 0)
  218. {
  219. continue;
  220. }
  221. nphs++;
  222. psize += alignup(ph->p_filesz, 16);
  223. }
  224. imgsize = sizeof(*header) + snlen + (nphs * 16) + psize;
  225. img = (char *)malloc(imgsize + 32);
  226. if (!img)
  227. {
  228. return NULL;
  229. }
  230. *image_size = imgsize;
  231. sym = elf->symtab;
  232. name = elf->string;
  233. sp = 0;
  234. for (j = 0; j < elf->nsyms; j++, sym++)
  235. {
  236. if (!strcmp("__stack", name + sym->st_name))
  237. {
  238. sp = sym->st_value;
  239. }
  240. }
  241. memset(img, 0, imgsize);
  242. header = (struct spk_header *)img;
  243. header->magic[0] = 0xef;
  244. header->magic[1] = 'M';
  245. header->magic[2] = 'O';
  246. header->magic[3] = 'D';
  247. header->cpu = core;
  248. header->entry = elf->ehdr->e_entry;
  249. header->stack = sp;
  250. header->core = core;
  251. header->binaries = nphs;
  252. header->phoffs = sizeof(*header) + snlen;
  253. header->mode = 0777;
  254. strncpy(img + sizeof(*header), savename, 63);
  255. ph = elf->phdr;
  256. pi = (struct spk_prog_info *)(img + header->phoffs);
  257. offset = ((char *)pi - img) + (nphs * sizeof(*pi));
  258. for (i = 0; i < elf->ehdr->e_phnum; i++, ph++)
  259. {
  260. if (ph->p_type != PT_LOAD || ph->p_filesz == 0)
  261. continue;
  262. pi->load_address = ph->p_paddr;
  263. pi->offset = offset;
  264. pi->size = alignup(ph->p_filesz, 16); /* need 16 bytes align for
  265. * decryption */
  266. pi->memsize = ph->p_memsz;
  267. memcpy(img + pi->offset, elf->data + ph->p_offset, ph->p_filesz);
  268. offset += alignup(ph->p_filesz, 16);
  269. pi++;
  270. }
  271. return img;
  272. }
  273. /****************************************************************************
  274. * Public Functions
  275. ****************************************************************************/
  276. int main(int argc, char **argv)
  277. {
  278. struct args *args;
  279. struct elf_file *elf;
  280. struct cipher *c;
  281. uint8_t *spkimage;
  282. int size = 0;
  283. FILE *fp;
  284. char footer[16];
  285. args = parse_args(argc, argv);
  286. elf = load_elf(args->elffile);
  287. if (!elf)
  288. {
  289. fprintf(stderr, "Loading ELF %s failure.\n", args->elffile);
  290. exit(EXIT_FAILURE);
  291. }
  292. spkimage = create_image(elf, args->core, args->savename, &size);
  293. free(elf);
  294. c = cipher_init(vmk, NULL);
  295. cipher_calc_cmac(c, spkimage, size, (uint8_t *) spkimage + size);
  296. cipher_deinit(c);
  297. size += 16; /* Extend CMAC size */
  298. snprintf(footer, 16, "MKSPK_BN_HOOTER");
  299. footer[15] = '\0';
  300. fp = fopen(args->outputfile, "wb");
  301. if (!fp)
  302. {
  303. fprintf(stderr, "Output file open error.\n");
  304. free(spkimage);
  305. exit(EXIT_FAILURE);
  306. }
  307. fwrite(spkimage, size, 1, fp);
  308. fwrite(footer, 16, 1, fp);
  309. fclose(fp);
  310. printf("File %s is successfully created.\n", args->outputfile);
  311. free(args->outputfile);
  312. memset(spkimage, 0, size);
  313. free(spkimage);
  314. exit(EXIT_SUCCESS);
  315. }