You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1121 lines
30 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007,2008 Øyvind Harboe *
  6. * oyvind.harboe@zylin.com *
  7. * *
  8. * Copyright (C) 2008 by Spencer Oliver *
  9. * spen@spen-soft.co.uk *
  10. * *
  11. * Copyright (C) 2009 by Franck Hereson *
  12. * franck.hereson@secad.fr *
  13. * *
  14. * This program is free software; you can redistribute it and/or modify *
  15. * it under the terms of the GNU General Public License as published by *
  16. * the Free Software Foundation; either version 2 of the License, or *
  17. * (at your option) any later version. *
  18. * *
  19. * This program is distributed in the hope that it will be useful, *
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  22. * GNU General Public License for more details. *
  23. * *
  24. * You should have received a copy of the GNU General Public License *
  25. * along with this program; if not, write to the *
  26. * Free Software Foundation, Inc., *
  27. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  28. ***************************************************************************/
  29. #ifdef HAVE_CONFIG_H
  30. #include "config.h"
  31. #endif
  32. #include "image.h"
  33. #include "target.h"
  34. #include <helper/log.h>
  35. /* convert ELF header field to host endianness */
  36. #define field16(elf,field)\
  37. ((elf->endianness == ELFDATA2LSB)? \
  38. le_to_h_u16((uint8_t*)&field):be_to_h_u16((uint8_t*)&field))
  39. #define field32(elf,field)\
  40. ((elf->endianness == ELFDATA2LSB)? \
  41. le_to_h_u32((uint8_t*)&field):be_to_h_u32((uint8_t*)&field))
  42. static int autodetect_image_type(struct image *image, const char *url)
  43. {
  44. int retval;
  45. struct fileio fileio;
  46. size_t read_bytes;
  47. uint8_t buffer[9];
  48. /* read the first 4 bytes of image */
  49. if ((retval = fileio_open(&fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
  50. {
  51. return retval;
  52. }
  53. retval = fileio_read(&fileio, 9, buffer, &read_bytes);
  54. if (retval == ERROR_OK)
  55. {
  56. if (read_bytes != 9)
  57. {
  58. retval = ERROR_FILEIO_OPERATION_FAILED;
  59. }
  60. }
  61. fileio_close(&fileio);
  62. if (retval != ERROR_OK)
  63. return retval;
  64. /* check header against known signatures */
  65. if (strncmp((char*)buffer,ELFMAG,SELFMAG) == 0)
  66. {
  67. LOG_DEBUG("ELF image detected.");
  68. image->type = IMAGE_ELF;
  69. }
  70. else if ((buffer[0]==':') /* record start byte */
  71. &&(isxdigit(buffer[1]))
  72. &&(isxdigit(buffer[2]))
  73. &&(isxdigit(buffer[3]))
  74. &&(isxdigit(buffer[4]))
  75. &&(isxdigit(buffer[5]))
  76. &&(isxdigit(buffer[6]))
  77. &&(buffer[7]=='0') /* record type : 00 -> 05 */
  78. &&(buffer[8]>='0') && (buffer[8]<'6'))
  79. {
  80. LOG_DEBUG("IHEX image detected.");
  81. image->type = IMAGE_IHEX;
  82. }
  83. else if ((buffer[0] == 'S') /* record start byte */
  84. &&(isxdigit(buffer[1]))
  85. &&(isxdigit(buffer[2]))
  86. &&(isxdigit(buffer[3]))
  87. &&(buffer[1] >= '0') && (buffer[1] < '9'))
  88. {
  89. LOG_DEBUG("S19 image detected.");
  90. image->type = IMAGE_SRECORD;
  91. }
  92. else
  93. {
  94. image->type = IMAGE_BINARY;
  95. }
  96. return ERROR_OK;
  97. }
  98. static int identify_image_type(struct image *image, const char *type_string, const char *url)
  99. {
  100. if (type_string)
  101. {
  102. if (!strcmp(type_string, "bin"))
  103. {
  104. image->type = IMAGE_BINARY;
  105. }
  106. else if (!strcmp(type_string, "ihex"))
  107. {
  108. image->type = IMAGE_IHEX;
  109. }
  110. else if (!strcmp(type_string, "elf"))
  111. {
  112. image->type = IMAGE_ELF;
  113. }
  114. else if (!strcmp(type_string, "mem"))
  115. {
  116. image->type = IMAGE_MEMORY;
  117. }
  118. else if (!strcmp(type_string, "s19"))
  119. {
  120. image->type = IMAGE_SRECORD;
  121. }
  122. else if (!strcmp(type_string, "build"))
  123. {
  124. image->type = IMAGE_BUILDER;
  125. }
  126. else
  127. {
  128. return ERROR_IMAGE_TYPE_UNKNOWN;
  129. }
  130. }
  131. else
  132. {
  133. return autodetect_image_type(image, url);
  134. }
  135. return ERROR_OK;
  136. }
  137. static int image_ihex_buffer_complete_inner(struct image *image, char *lpszLine, struct imageection *section)
  138. {
  139. struct image_ihex *ihex = image->type_private;
  140. struct fileio *fileio = &ihex->fileio;
  141. uint32_t full_address = 0x0;
  142. uint32_t cooked_bytes;
  143. int i;
  144. /* we can't determine the number of sections that we'll have to create ahead of time,
  145. * so we locally hold them until parsing is finished */
  146. ihex->buffer = malloc(fileio->size >> 1);
  147. cooked_bytes = 0x0;
  148. image->num_sections = 0;
  149. section[image->num_sections].private = &ihex->buffer[cooked_bytes];
  150. section[image->num_sections].base_address = 0x0;
  151. section[image->num_sections].size = 0x0;
  152. section[image->num_sections].flags = 0;
  153. while (fileio_fgets(fileio, 1023, lpszLine) == ERROR_OK)
  154. {
  155. uint32_t count;
  156. uint32_t address;
  157. uint32_t record_type;
  158. uint32_t checksum;
  159. uint8_t cal_checksum = 0;
  160. size_t bytes_read = 0;
  161. if (sscanf(&lpszLine[bytes_read], ":%2" SCNx32 "%4" SCNx32 "%2" SCNx32 , &count, &address, &record_type) != 3)
  162. {
  163. return ERROR_IMAGE_FORMAT_ERROR;
  164. }
  165. bytes_read += 9;
  166. cal_checksum += (uint8_t)count;
  167. cal_checksum += (uint8_t)(address >> 8);
  168. cal_checksum += (uint8_t)address;
  169. cal_checksum += (uint8_t)record_type;
  170. if (record_type == 0) /* Data Record */
  171. {
  172. if ((full_address & 0xffff) != address)
  173. {
  174. /* we encountered a nonconsecutive location, create a new section,
  175. * unless the current section has zero size, in which case this specifies
  176. * the current section's base address
  177. */
  178. if (section[image->num_sections].size != 0)
  179. {
  180. image->num_sections++;
  181. if (image->num_sections >= IMAGE_MAX_SECTIONS)
  182. {
  183. /* too many sections */
  184. LOG_ERROR("Too many sections found in IHEX file");
  185. return ERROR_IMAGE_FORMAT_ERROR;
  186. }
  187. section[image->num_sections].size = 0x0;
  188. section[image->num_sections].flags = 0;
  189. section[image->num_sections].private = &ihex->buffer[cooked_bytes];
  190. }
  191. section[image->num_sections].base_address =
  192. (full_address & 0xffff0000) | address;
  193. full_address = (full_address & 0xffff0000) | address;
  194. }
  195. while (count-- > 0)
  196. {
  197. unsigned value;
  198. sscanf(&lpszLine[bytes_read], "%2x", &value);
  199. ihex->buffer[cooked_bytes] = (uint8_t)value;
  200. cal_checksum += (uint8_t)ihex->buffer[cooked_bytes];
  201. bytes_read += 2;
  202. cooked_bytes += 1;
  203. section[image->num_sections].size += 1;
  204. full_address++;
  205. }
  206. }
  207. else if (record_type == 1) /* End of File Record */
  208. {
  209. /* finish the current section */
  210. image->num_sections++;
  211. /* copy section information */
  212. image->sections = malloc(sizeof(struct imageection) * image->num_sections);
  213. for (i = 0; i < image->num_sections; i++)
  214. {
  215. image->sections[i].private = section[i].private;
  216. image->sections[i].base_address = section[i].base_address;
  217. image->sections[i].size = section[i].size;
  218. image->sections[i].flags = section[i].flags;
  219. }
  220. return ERROR_OK;
  221. }
  222. else if (record_type == 2) /* Linear Address Record */
  223. {
  224. uint16_t upper_address;
  225. sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
  226. cal_checksum += (uint8_t)(upper_address >> 8);
  227. cal_checksum += (uint8_t)upper_address;
  228. bytes_read += 4;
  229. if ((full_address >> 4) != upper_address)
  230. {
  231. /* we encountered a nonconsecutive location, create a new section,
  232. * unless the current section has zero size, in which case this specifies
  233. * the current section's base address
  234. */
  235. if (section[image->num_sections].size != 0)
  236. {
  237. image->num_sections++;
  238. if (image->num_sections >= IMAGE_MAX_SECTIONS)
  239. {
  240. /* too many sections */
  241. LOG_ERROR("Too many sections found in IHEX file");
  242. return ERROR_IMAGE_FORMAT_ERROR;
  243. }
  244. section[image->num_sections].size = 0x0;
  245. section[image->num_sections].flags = 0;
  246. section[image->num_sections].private = &ihex->buffer[cooked_bytes];
  247. }
  248. section[image->num_sections].base_address =
  249. (full_address & 0xffff) | (upper_address << 4);
  250. full_address = (full_address & 0xffff) | (upper_address << 4);
  251. }
  252. }
  253. else if (record_type == 3) /* Start Segment Address Record */
  254. {
  255. uint32_t dummy;
  256. /* "Start Segment Address Record" will not be supported */
  257. /* but we must consume it, and do not create an error. */
  258. while (count-- > 0)
  259. {
  260. sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &dummy);
  261. cal_checksum += (uint8_t)dummy;
  262. bytes_read += 2;
  263. }
  264. }
  265. else if (record_type == 4) /* Extended Linear Address Record */
  266. {
  267. uint16_t upper_address;
  268. sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
  269. cal_checksum += (uint8_t)(upper_address >> 8);
  270. cal_checksum += (uint8_t)upper_address;
  271. bytes_read += 4;
  272. if ((full_address >> 16) != upper_address)
  273. {
  274. /* we encountered a nonconsecutive location, create a new section,
  275. * unless the current section has zero size, in which case this specifies
  276. * the current section's base address
  277. */
  278. if (section[image->num_sections].size != 0)
  279. {
  280. image->num_sections++;
  281. if (image->num_sections >= IMAGE_MAX_SECTIONS)
  282. {
  283. /* too many sections */
  284. LOG_ERROR("Too many sections found in IHEX file");
  285. return ERROR_IMAGE_FORMAT_ERROR;
  286. }
  287. section[image->num_sections].size = 0x0;
  288. section[image->num_sections].flags = 0;
  289. section[image->num_sections].private = &ihex->buffer[cooked_bytes];
  290. }
  291. section[image->num_sections].base_address =
  292. (full_address & 0xffff) | (upper_address << 16);
  293. full_address = (full_address & 0xffff) | (upper_address << 16);
  294. }
  295. }
  296. else if (record_type == 5) /* Start Linear Address Record */
  297. {
  298. uint32_t start_address;
  299. sscanf(&lpszLine[bytes_read], "%8" SCNx32, &start_address);
  300. cal_checksum += (uint8_t)(start_address >> 24);
  301. cal_checksum += (uint8_t)(start_address >> 16);
  302. cal_checksum += (uint8_t)(start_address >> 8);
  303. cal_checksum += (uint8_t)start_address;
  304. bytes_read += 8;
  305. image->start_address_set = 1;
  306. image->start_address = be_to_h_u32((uint8_t*)&start_address);
  307. }
  308. else
  309. {
  310. LOG_ERROR("unhandled IHEX record type: %i", (int)record_type);
  311. return ERROR_IMAGE_FORMAT_ERROR;
  312. }
  313. sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &checksum);
  314. bytes_read += 2;
  315. if ((uint8_t)checksum != (uint8_t)(~cal_checksum + 1))
  316. {
  317. /* checksum failed */
  318. LOG_ERROR("incorrect record checksum found in IHEX file");
  319. return ERROR_IMAGE_CHECKSUM;
  320. }
  321. }
  322. LOG_ERROR("premature end of IHEX file, no end-of-file record found");
  323. return ERROR_IMAGE_FORMAT_ERROR;
  324. }
  325. /**
  326. * Allocate memory dynamically instead of on the stack. This
  327. * is important w/embedded hosts.
  328. */
  329. static int image_ihex_buffer_complete(struct image *image)
  330. {
  331. char *lpszLine = malloc(1023);
  332. if (lpszLine == NULL)
  333. {
  334. LOG_ERROR("Out of memory");
  335. return ERROR_FAIL;
  336. }
  337. struct imageection *section = malloc(sizeof(struct imageection) * IMAGE_MAX_SECTIONS);
  338. if (section == NULL)
  339. {
  340. free(lpszLine);
  341. LOG_ERROR("Out of memory");
  342. return ERROR_FAIL;
  343. }
  344. int retval;
  345. retval = image_ihex_buffer_complete_inner(image, lpszLine, section);
  346. free(section);
  347. free(lpszLine);
  348. return retval;
  349. }
  350. static int image_elf_read_headers(struct image *image)
  351. {
  352. struct image_elf *elf = image->type_private;
  353. size_t read_bytes;
  354. uint32_t i,j;
  355. int retval;
  356. elf->header = malloc(sizeof(Elf32_Ehdr));
  357. if (elf->header == NULL)
  358. {
  359. LOG_ERROR("insufficient memory to perform operation ");
  360. return ERROR_FILEIO_OPERATION_FAILED;
  361. }
  362. if ((retval = fileio_read(&elf->fileio, sizeof(Elf32_Ehdr), (uint8_t*)elf->header, &read_bytes)) != ERROR_OK)
  363. {
  364. LOG_ERROR("cannot read ELF file header, read failed");
  365. return ERROR_FILEIO_OPERATION_FAILED;
  366. }
  367. if (read_bytes != sizeof(Elf32_Ehdr))
  368. {
  369. LOG_ERROR("cannot read ELF file header, only partially read");
  370. return ERROR_FILEIO_OPERATION_FAILED;
  371. }
  372. if (strncmp((char*)elf->header->e_ident,ELFMAG,SELFMAG) != 0)
  373. {
  374. LOG_ERROR("invalid ELF file, bad magic number");
  375. return ERROR_IMAGE_FORMAT_ERROR;
  376. }
  377. if (elf->header->e_ident[EI_CLASS]!=ELFCLASS32)
  378. {
  379. LOG_ERROR("invalid ELF file, only 32bits files are supported");
  380. return ERROR_IMAGE_FORMAT_ERROR;
  381. }
  382. elf->endianness = elf->header->e_ident[EI_DATA];
  383. if ((elf->endianness != ELFDATA2LSB)
  384. &&(elf->endianness != ELFDATA2MSB))
  385. {
  386. LOG_ERROR("invalid ELF file, unknown endianess setting");
  387. return ERROR_IMAGE_FORMAT_ERROR;
  388. }
  389. elf->segment_count = field16(elf,elf->header->e_phnum);
  390. if (elf->segment_count == 0)
  391. {
  392. LOG_ERROR("invalid ELF file, no program headers");
  393. return ERROR_IMAGE_FORMAT_ERROR;
  394. }
  395. if ((retval = fileio_seek(&elf->fileio, field32(elf,elf->header->e_phoff))) != ERROR_OK)
  396. {
  397. LOG_ERROR("cannot seek to ELF program header table, read failed");
  398. return retval;
  399. }
  400. elf->segments = malloc(elf->segment_count*sizeof(Elf32_Phdr));
  401. if (elf->segments == NULL)
  402. {
  403. LOG_ERROR("insufficient memory to perform operation ");
  404. return ERROR_FILEIO_OPERATION_FAILED;
  405. }
  406. if ((retval = fileio_read(&elf->fileio, elf->segment_count*sizeof(Elf32_Phdr), (uint8_t*)elf->segments, &read_bytes)) != ERROR_OK)
  407. {
  408. LOG_ERROR("cannot read ELF segment headers, read failed");
  409. return retval;
  410. }
  411. if (read_bytes != elf->segment_count*sizeof(Elf32_Phdr))
  412. {
  413. LOG_ERROR("cannot read ELF segment headers, only partially read");
  414. return ERROR_FILEIO_OPERATION_FAILED;
  415. }
  416. /* count useful segments (loadable), ignore BSS section */
  417. image->num_sections = 0;
  418. for (i = 0;i < elf->segment_count;i++)
  419. if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
  420. image->num_sections++;
  421. /* alloc and fill sections array with loadable segments */
  422. image->sections = malloc(image->num_sections * sizeof(struct imageection));
  423. for (i = 0,j = 0;i < elf->segment_count;i++)
  424. {
  425. if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
  426. {
  427. image->sections[j].size = field32(elf,elf->segments[i].p_filesz);
  428. image->sections[j].base_address = field32(elf,elf->segments[i].p_paddr);
  429. image->sections[j].private = &elf->segments[i];
  430. image->sections[j].flags = field32(elf,elf->segments[i].p_flags);
  431. j++;
  432. }
  433. }
  434. image->start_address_set = 1;
  435. image->start_address = field32(elf,elf->header->e_entry);
  436. return ERROR_OK;
  437. }
  438. static int image_elf_read_section(struct image *image, int section, uint32_t offset, uint32_t size, uint8_t *buffer, size_t *size_read)
  439. {
  440. struct image_elf *elf = image->type_private;
  441. Elf32_Phdr *segment = (Elf32_Phdr *)image->sections[section].private;
  442. size_t read_size,really_read;
  443. int retval;
  444. *size_read = 0;
  445. LOG_DEBUG("load segment %d at 0x%" PRIx32 " (sz = 0x%" PRIx32 ")",section,offset,size);
  446. /* read initialized data in current segment if any */
  447. if (offset < field32(elf,segment->p_filesz))
  448. {
  449. /* maximal size present in file for the current segment */
  450. read_size = MIN(size, field32(elf,segment->p_filesz)-offset);
  451. LOG_DEBUG("read elf: size = 0x%zu at 0x%" PRIx32 "", read_size,
  452. field32(elf,segment->p_offset) + offset);
  453. /* read initialized area of the segment */
  454. if ((retval = fileio_seek(&elf->fileio, field32(elf,segment->p_offset) + offset)) != ERROR_OK)
  455. {
  456. LOG_ERROR("cannot find ELF segment content, seek failed");
  457. return retval;
  458. }
  459. if ((retval = fileio_read(&elf->fileio, read_size, buffer, &really_read)) != ERROR_OK)
  460. {
  461. LOG_ERROR("cannot read ELF segment content, read failed");
  462. return retval;
  463. }
  464. buffer += read_size;
  465. size -= read_size;
  466. offset += read_size;
  467. *size_read += read_size;
  468. /* need more data ? */
  469. if (!size)
  470. return ERROR_OK;
  471. }
  472. return ERROR_OK;
  473. }
  474. static int image_mot_buffer_complete_inner(struct image *image, char *lpszLine, struct imageection *section)
  475. {
  476. struct image_mot *mot = image->type_private;
  477. struct fileio *fileio = &mot->fileio;
  478. uint32_t full_address = 0x0;
  479. uint32_t cooked_bytes;
  480. int i;
  481. /* we can't determine the number of sections that we'll have to create ahead of time,
  482. * so we locally hold them until parsing is finished */
  483. mot->buffer = malloc(fileio->size >> 1);
  484. cooked_bytes = 0x0;
  485. image->num_sections = 0;
  486. section[image->num_sections].private = &mot->buffer[cooked_bytes];
  487. section[image->num_sections].base_address = 0x0;
  488. section[image->num_sections].size = 0x0;
  489. section[image->num_sections].flags = 0;
  490. while (fileio_fgets(fileio, 1023, lpszLine) == ERROR_OK)
  491. {
  492. uint32_t count;
  493. uint32_t address;
  494. uint32_t record_type;
  495. uint32_t checksum;
  496. uint8_t cal_checksum = 0;
  497. uint32_t bytes_read = 0;
  498. /* get record type and record length */
  499. if (sscanf(&lpszLine[bytes_read], "S%1" SCNx32 "%2" SCNx32 , &record_type, &count) != 2)
  500. {
  501. return ERROR_IMAGE_FORMAT_ERROR;
  502. }
  503. bytes_read += 4;
  504. cal_checksum += (uint8_t)count;
  505. /* skip checksum byte */
  506. count -=1;
  507. if (record_type == 0)
  508. {
  509. /* S0 - starting record (optional) */
  510. int iValue;
  511. while (count-- > 0) {
  512. sscanf(&lpszLine[bytes_read], "%2x", &iValue);
  513. cal_checksum += (uint8_t)iValue;
  514. bytes_read += 2;
  515. }
  516. }
  517. else if (record_type >= 1 && record_type <= 3)
  518. {
  519. switch (record_type)
  520. {
  521. case 1:
  522. /* S1 - 16 bit address data record */
  523. sscanf(&lpszLine[bytes_read], "%4" SCNx32, &address);
  524. cal_checksum += (uint8_t)(address >> 8);
  525. cal_checksum += (uint8_t)address;
  526. bytes_read += 4;
  527. count -=2;
  528. break;
  529. case 2:
  530. /* S2 - 24 bit address data record */
  531. sscanf(&lpszLine[bytes_read], "%6" SCNx32 , &address);
  532. cal_checksum += (uint8_t)(address >> 16);
  533. cal_checksum += (uint8_t)(address >> 8);
  534. cal_checksum += (uint8_t)address;
  535. bytes_read += 6;
  536. count -=3;
  537. break;
  538. case 3:
  539. /* S3 - 32 bit address data record */
  540. sscanf(&lpszLine[bytes_read], "%8" SCNx32 , &address);
  541. cal_checksum += (uint8_t)(address >> 24);
  542. cal_checksum += (uint8_t)(address >> 16);
  543. cal_checksum += (uint8_t)(address >> 8);
  544. cal_checksum += (uint8_t)address;
  545. bytes_read += 8;
  546. count -=4;
  547. break;
  548. }
  549. if (full_address != address)
  550. {
  551. /* we encountered a nonconsecutive location, create a new section,
  552. * unless the current section has zero size, in which case this specifies
  553. * the current section's base address
  554. */
  555. if (section[image->num_sections].size != 0)
  556. {
  557. image->num_sections++;
  558. section[image->num_sections].size = 0x0;
  559. section[image->num_sections].flags = 0;
  560. section[image->num_sections].private = &mot->buffer[cooked_bytes];
  561. }
  562. section[image->num_sections].base_address = address;
  563. full_address = address;
  564. }
  565. while (count-- > 0)
  566. {
  567. unsigned value;
  568. sscanf(&lpszLine[bytes_read], "%2x", &value);
  569. mot->buffer[cooked_bytes] = (uint8_t)value;
  570. cal_checksum += (uint8_t)mot->buffer[cooked_bytes];
  571. bytes_read += 2;
  572. cooked_bytes += 1;
  573. section[image->num_sections].size += 1;
  574. full_address++;
  575. }
  576. }
  577. else if (record_type == 5)
  578. {
  579. /* S5 is the data count record, we ignore it */
  580. uint32_t dummy;
  581. while (count-- > 0)
  582. {
  583. sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &dummy);
  584. cal_checksum += (uint8_t)dummy;
  585. bytes_read += 2;
  586. }
  587. }
  588. else if (record_type >= 7 && record_type <= 9)
  589. {
  590. /* S7, S8, S9 - ending records for 32, 24 and 16bit */
  591. image->num_sections++;
  592. /* copy section information */
  593. image->sections = malloc(sizeof(struct imageection) * image->num_sections);
  594. for (i = 0; i < image->num_sections; i++)
  595. {
  596. image->sections[i].private = section[i].private;
  597. image->sections[i].base_address = section[i].base_address;
  598. image->sections[i].size = section[i].size;
  599. image->sections[i].flags = section[i].flags;
  600. }
  601. return ERROR_OK;
  602. }
  603. else
  604. {
  605. LOG_ERROR("unhandled S19 record type: %i", (int)(record_type));
  606. return ERROR_IMAGE_FORMAT_ERROR;
  607. }
  608. /* account for checksum, will always be 0xFF */
  609. sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &checksum);
  610. cal_checksum += (uint8_t)checksum;
  611. bytes_read += 2;
  612. if (cal_checksum != 0xFF)
  613. {
  614. /* checksum failed */
  615. LOG_ERROR("incorrect record checksum found in S19 file");
  616. return ERROR_IMAGE_CHECKSUM;
  617. }
  618. }
  619. LOG_ERROR("premature end of S19 file, no end-of-file record found");
  620. return ERROR_IMAGE_FORMAT_ERROR;
  621. }
  622. /**
  623. * Allocate memory dynamically instead of on the stack. This
  624. * is important w/embedded hosts.
  625. */
  626. static int image_mot_buffer_complete(struct image *image)
  627. {
  628. char *lpszLine = malloc(1023);
  629. if (lpszLine == NULL)
  630. {
  631. LOG_ERROR("Out of memory");
  632. return ERROR_FAIL;
  633. }
  634. struct imageection *section = malloc(sizeof(struct imageection) * IMAGE_MAX_SECTIONS);
  635. if (section == NULL)
  636. {
  637. free(lpszLine);
  638. LOG_ERROR("Out of memory");
  639. return ERROR_FAIL;
  640. }
  641. int retval;
  642. retval = image_mot_buffer_complete_inner(image, lpszLine, section);
  643. free(section);
  644. free(lpszLine);
  645. return retval;
  646. }
  647. int image_open(struct image *image, const char *url, const char *type_string)
  648. {
  649. int retval = ERROR_OK;
  650. if ((retval = identify_image_type(image, type_string, url)) != ERROR_OK)
  651. {
  652. return retval;
  653. }
  654. if (image->type == IMAGE_BINARY)
  655. {
  656. struct image_binary *image_binary;
  657. image_binary = image->type_private = malloc(sizeof(struct image_binary));
  658. if ((retval = fileio_open(&image_binary->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
  659. {
  660. return retval;
  661. }
  662. image->num_sections = 1;
  663. image->sections = malloc(sizeof(struct imageection));
  664. image->sections[0].base_address = 0x0;
  665. image->sections[0].size = image_binary->fileio.size;
  666. image->sections[0].flags = 0;
  667. }
  668. else if (image->type == IMAGE_IHEX)
  669. {
  670. struct image_ihex *image_ihex;
  671. image_ihex = image->type_private = malloc(sizeof(struct image_ihex));
  672. if ((retval = fileio_open(&image_ihex->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
  673. {
  674. return retval;
  675. }
  676. if ((retval = image_ihex_buffer_complete(image)) != ERROR_OK)
  677. {
  678. LOG_ERROR("failed buffering IHEX image, check daemon output for additional information");
  679. fileio_close(&image_ihex->fileio);
  680. return retval;
  681. }
  682. }
  683. else if (image->type == IMAGE_ELF)
  684. {
  685. struct image_elf *image_elf;
  686. image_elf = image->type_private = malloc(sizeof(struct image_elf));
  687. if ((retval = fileio_open(&image_elf->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
  688. {
  689. return retval;
  690. }
  691. if ((retval = image_elf_read_headers(image)) != ERROR_OK)
  692. {
  693. fileio_close(&image_elf->fileio);
  694. return retval;
  695. }
  696. }
  697. else if (image->type == IMAGE_MEMORY)
  698. {
  699. struct target *target = get_target(url);
  700. if (target == NULL)
  701. {
  702. LOG_ERROR("target '%s' not defined", url);
  703. return ERROR_FAIL;
  704. }
  705. struct image_memory *image_memory;
  706. image->num_sections = 1;
  707. image->sections = malloc(sizeof(struct imageection));
  708. image->sections[0].base_address = 0x0;
  709. image->sections[0].size = 0xffffffff;
  710. image->sections[0].flags = 0;
  711. image_memory = image->type_private = malloc(sizeof(struct image_memory));
  712. image_memory->target = target;
  713. image_memory->cache = NULL;
  714. image_memory->cache_address = 0x0;
  715. }
  716. else if (image->type == IMAGE_SRECORD)
  717. {
  718. struct image_mot *image_mot;
  719. image_mot = image->type_private = malloc(sizeof(struct image_mot));
  720. if ((retval = fileio_open(&image_mot->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
  721. {
  722. return retval;
  723. }
  724. if ((retval = image_mot_buffer_complete(image)) != ERROR_OK)
  725. {
  726. LOG_ERROR("failed buffering S19 image, check daemon output for additional information");
  727. fileio_close(&image_mot->fileio);
  728. return retval;
  729. }
  730. }
  731. else if (image->type == IMAGE_BUILDER)
  732. {
  733. image->num_sections = 0;
  734. image->sections = NULL;
  735. image->type_private = NULL;
  736. }
  737. if (image->base_address_set)
  738. {
  739. /* relocate */
  740. int section;
  741. for (section = 0; section < image->num_sections; section++)
  742. {
  743. image->sections[section].base_address += image->base_address;
  744. }
  745. /* we're done relocating. The two statements below are mainly
  746. * for documenation purposes: stop anyone from empirically
  747. * thinking they should use these values henceforth. */
  748. image->base_address = 0;
  749. image->base_address_set = 0;
  750. }
  751. return retval;
  752. };
  753. int image_read_section(struct image *image, int section, uint32_t offset, uint32_t size, uint8_t *buffer, size_t *size_read)
  754. {
  755. int retval;
  756. /* don't read past the end of a section */
  757. if (offset + size > image->sections[section].size)
  758. {
  759. LOG_DEBUG("read past end of section: 0x%8.8" PRIx32 " + 0x%8.8" PRIx32 " > 0x%8.8" PRIx32 "",
  760. offset, size, image->sections[section].size);
  761. return ERROR_INVALID_ARGUMENTS;
  762. }
  763. if (image->type == IMAGE_BINARY)
  764. {
  765. struct image_binary *image_binary = image->type_private;
  766. /* only one section in a plain binary */
  767. if (section != 0)
  768. return ERROR_INVALID_ARGUMENTS;
  769. /* seek to offset */
  770. if ((retval = fileio_seek(&image_binary->fileio, offset)) != ERROR_OK)
  771. {
  772. return retval;
  773. }
  774. /* return requested bytes */
  775. if ((retval = fileio_read(&image_binary->fileio, size, buffer, size_read)) != ERROR_OK)
  776. {
  777. return retval;
  778. }
  779. }
  780. else if (image->type == IMAGE_IHEX)
  781. {
  782. memcpy(buffer, (uint8_t*)image->sections[section].private + offset, size);
  783. *size_read = size;
  784. return ERROR_OK;
  785. }
  786. else if (image->type == IMAGE_ELF)
  787. {
  788. return image_elf_read_section(image, section, offset, size, buffer, size_read);
  789. }
  790. else if (image->type == IMAGE_MEMORY)
  791. {
  792. struct image_memory *image_memory = image->type_private;
  793. uint32_t address = image->sections[section].base_address + offset;
  794. *size_read = 0;
  795. while ((size - *size_read) > 0)
  796. {
  797. uint32_t size_in_cache;
  798. if (!image_memory->cache
  799. || (address < image_memory->cache_address)
  800. || (address >= (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE)))
  801. {
  802. if (!image_memory->cache)
  803. image_memory->cache = malloc(IMAGE_MEMORY_CACHE_SIZE);
  804. if (target_read_buffer(image_memory->target, address & ~(IMAGE_MEMORY_CACHE_SIZE - 1),
  805. IMAGE_MEMORY_CACHE_SIZE, image_memory->cache) != ERROR_OK)
  806. {
  807. free(image_memory->cache);
  808. image_memory->cache = NULL;
  809. return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
  810. }
  811. image_memory->cache_address = address & ~(IMAGE_MEMORY_CACHE_SIZE - 1);
  812. }
  813. size_in_cache = (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE) - address;
  814. memcpy(buffer + *size_read,
  815. image_memory->cache + (address - image_memory->cache_address),
  816. (size_in_cache > size) ? size : size_in_cache
  817. );
  818. *size_read += (size_in_cache > size) ? size : size_in_cache;
  819. address += (size_in_cache > size) ? size : size_in_cache;
  820. }
  821. }
  822. else if (image->type == IMAGE_SRECORD)
  823. {
  824. memcpy(buffer, (uint8_t*)image->sections[section].private + offset, size);
  825. *size_read = size;
  826. return ERROR_OK;
  827. }
  828. else if (image->type == IMAGE_BUILDER)
  829. {
  830. memcpy(buffer, (uint8_t*)image->sections[section].private + offset, size);
  831. *size_read = size;
  832. return ERROR_OK;
  833. }
  834. return ERROR_OK;
  835. }
  836. int image_add_section(struct image *image, uint32_t base, uint32_t size, int flags, uint8_t *data)
  837. {
  838. struct imageection *section;
  839. /* only image builder supports adding sections */
  840. if (image->type != IMAGE_BUILDER)
  841. return ERROR_INVALID_ARGUMENTS;
  842. /* see if there's a previous section */
  843. if (image->num_sections)
  844. {
  845. section = &image->sections[image->num_sections - 1];
  846. /* see if it's enough to extend the last section,
  847. * adding data to previous sections or merging is not supported */
  848. if (((section->base_address + section->size) == base) && (section->flags == flags))
  849. {
  850. section->private = realloc(section->private, section->size + size);
  851. memcpy((uint8_t*)section->private + section->size, data, size);
  852. section->size += size;
  853. return ERROR_OK;
  854. }
  855. }
  856. /* allocate new section */
  857. image->num_sections++;
  858. image->sections = realloc(image->sections, sizeof(struct imageection) * image->num_sections);
  859. section = &image->sections[image->num_sections - 1];
  860. section->base_address = base;
  861. section->size = size;
  862. section->flags = flags;
  863. section->private = malloc(sizeof(uint8_t) * size);
  864. memcpy((uint8_t*)section->private, data, size);
  865. return ERROR_OK;
  866. }
  867. void image_close(struct image *image)
  868. {
  869. if (image->type == IMAGE_BINARY)
  870. {
  871. struct image_binary *image_binary = image->type_private;
  872. fileio_close(&image_binary->fileio);
  873. }
  874. else if (image->type == IMAGE_IHEX)
  875. {
  876. struct image_ihex *image_ihex = image->type_private;
  877. fileio_close(&image_ihex->fileio);
  878. if (image_ihex->buffer)
  879. {
  880. free(image_ihex->buffer);
  881. image_ihex->buffer = NULL;
  882. }
  883. }
  884. else if (image->type == IMAGE_ELF)
  885. {
  886. struct image_elf *image_elf = image->type_private;
  887. fileio_close(&image_elf->fileio);
  888. if (image_elf->header)
  889. {
  890. free(image_elf->header);
  891. image_elf->header = NULL;
  892. }
  893. if (image_elf->segments)
  894. {
  895. free(image_elf->segments);
  896. image_elf->segments = NULL;
  897. }
  898. }
  899. else if (image->type == IMAGE_MEMORY)
  900. {
  901. struct image_memory *image_memory = image->type_private;
  902. if (image_memory->cache)
  903. {
  904. free(image_memory->cache);
  905. image_memory->cache = NULL;
  906. }
  907. }
  908. else if (image->type == IMAGE_SRECORD)
  909. {
  910. struct image_mot *image_mot = image->type_private;
  911. fileio_close(&image_mot->fileio);
  912. if (image_mot->buffer)
  913. {
  914. free(image_mot->buffer);
  915. image_mot->buffer = NULL;
  916. }
  917. }
  918. else if (image->type == IMAGE_BUILDER)
  919. {
  920. int i;
  921. for (i = 0; i < image->num_sections; i++)
  922. {
  923. free(image->sections[i].private);
  924. image->sections[i].private = NULL;
  925. }
  926. }
  927. if (image->type_private)
  928. {
  929. free(image->type_private);
  930. image->type_private = NULL;
  931. }
  932. if (image->sections)
  933. {
  934. free(image->sections);
  935. image->sections = NULL;
  936. }
  937. }
  938. int image_calculate_checksum(uint8_t* buffer, uint32_t nbytes, uint32_t* checksum)
  939. {
  940. uint32_t crc = 0xffffffff;
  941. LOG_DEBUG("Calculating checksum");
  942. static uint32_t crc32_table[256];
  943. static bool first_init = false;
  944. if (!first_init)
  945. {
  946. /* Initialize the CRC table and the decoding table. */
  947. int i, j;
  948. unsigned int c;
  949. for (i = 0; i < 256; i++)
  950. {
  951. /* as per gdb */
  952. for (c = i << 24, j = 8; j > 0; --j)
  953. c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
  954. crc32_table[i] = c;
  955. }
  956. first_init = true;
  957. }
  958. while (nbytes > 0)
  959. {
  960. int run = nbytes;
  961. if (run > 32768)
  962. {
  963. run = 32768;
  964. }
  965. nbytes -= run;
  966. while (run--)
  967. {
  968. /* as per gdb */
  969. crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buffer++) & 255];
  970. }
  971. keep_alive();
  972. }
  973. LOG_DEBUG("Calculating checksum done");
  974. *checksum = crc;
  975. return ERROR_OK;
  976. }