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.
 
 
 
 
 
 

652 lines
18 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #ifdef HAVE_ELF_H
  26. #include <elf.h>
  27. #endif
  28. #include "image.h"
  29. #include "types.h"
  30. #include "replacements.h"
  31. #include "log.h"
  32. #include "fileio.h"
  33. #include "target.h"
  34. /* convert ELF header field to host endianness */
  35. #define field16(elf,field)\
  36. ((elf->endianness==ELFDATA2LSB)? \
  37. le_to_h_u16((u8*)&field):be_to_h_u16((u8*)&field))
  38. #define field32(elf,field)\
  39. ((elf->endianness==ELFDATA2LSB)? \
  40. le_to_h_u32((u8*)&field):be_to_h_u32((u8*)&field))
  41. static int autodetect_image_type(image_t *image, char *url)
  42. {
  43. int retval;
  44. fileio_t fileio;
  45. u32 read_bytes;
  46. u8 buffer[9];
  47. /* read the first 4 bytes of image */
  48. if ((retval = fileio_open(&fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
  49. {
  50. snprintf(image->error_str, IMAGE_MAX_ERROR_STRING, "cannot open image: %s", fileio.error_str);
  51. ERROR(image->error_str);
  52. return retval;
  53. }
  54. if ((retval = fileio_read(&fileio, 9, buffer, &read_bytes)) != ERROR_OK)
  55. {
  56. snprintf(image->error_str, IMAGE_MAX_ERROR_STRING, "cannot read image header: %s", fileio.error_str);
  57. ERROR(image->error_str);
  58. return ERROR_FILEIO_OPERATION_FAILED;
  59. }
  60. if (read_bytes != 9)
  61. {
  62. snprintf(image->error_str, IMAGE_MAX_ERROR_STRING, "cannot read image, only partially read");
  63. ERROR(image->error_str);
  64. return ERROR_FILEIO_OPERATION_FAILED;
  65. }
  66. fileio_close(&fileio);
  67. /* check header against known signatures */
  68. if (strncmp((char*)buffer,ELFMAG,SELFMAG)==0)
  69. {
  70. DEBUG("ELF image detected.");
  71. image->type = IMAGE_ELF;
  72. }
  73. else if ((buffer[0]==':') /* record start byte */
  74. &&(isxdigit(buffer[1]))
  75. &&(isxdigit(buffer[2]))
  76. &&(isxdigit(buffer[3]))
  77. &&(isxdigit(buffer[4]))
  78. &&(isxdigit(buffer[5]))
  79. &&(isxdigit(buffer[6]))
  80. &&(buffer[7]=='0') /* record type : 00 -> 05 */
  81. &&(buffer[8]>='0')&&(buffer[8]<'6'))
  82. {
  83. DEBUG("IHEX image detected.");
  84. image->type = IMAGE_IHEX;
  85. }
  86. else
  87. {
  88. image->type = IMAGE_BINARY;
  89. }
  90. return ERROR_OK;
  91. }
  92. int identify_image_type(image_t *image, char *type_string, char *url)
  93. {
  94. if (type_string)
  95. {
  96. if (!strcmp(type_string, "bin"))
  97. {
  98. image->type = IMAGE_BINARY;
  99. }
  100. else if (!strcmp(type_string, "ihex"))
  101. {
  102. image->type = IMAGE_IHEX;
  103. }
  104. else if (!strcmp(type_string, "elf"))
  105. {
  106. image->type = IMAGE_ELF;
  107. }
  108. else if (!strcmp(type_string, "mem"))
  109. {
  110. image->type = IMAGE_MEMORY;
  111. }
  112. else
  113. {
  114. return ERROR_IMAGE_TYPE_UNKNOWN;
  115. }
  116. }
  117. else
  118. {
  119. return autodetect_image_type(image, url);
  120. }
  121. return ERROR_OK;
  122. }
  123. int image_ihex_buffer_complete(image_t *image)
  124. {
  125. image_ihex_t *ihex = image->type_private;
  126. fileio_t *fileio = &ihex->fileio;
  127. u32 raw_bytes_read, raw_bytes;
  128. int retval;
  129. u32 full_address = 0x0;
  130. char *buffer = malloc(fileio->size);
  131. u32 cooked_bytes;
  132. int i;
  133. /* we can't determine the number of sections that we'll have to create ahead of time,
  134. * so we locally hold them until parsing is finished */
  135. image_section_t section[IMAGE_MAX_SECTIONS];
  136. if ((retval = fileio_read(fileio, fileio->size, (u8*)buffer, &raw_bytes_read)) != ERROR_OK)
  137. {
  138. free(buffer);
  139. ERROR("failed buffering IHEX file, read failed");
  140. return ERROR_FILEIO_OPERATION_FAILED;
  141. }
  142. if (raw_bytes_read != fileio->size)
  143. {
  144. free(buffer);
  145. ERROR("failed buffering complete IHEX file, only partially read");
  146. return ERROR_FILEIO_OPERATION_FAILED;
  147. }
  148. ihex->buffer = malloc(fileio->size >> 1);
  149. raw_bytes = 0x0;
  150. cooked_bytes = 0x0;
  151. image->num_sections = 0;
  152. section[image->num_sections].private = &ihex->buffer[cooked_bytes];
  153. section[image->num_sections].base_address = 0x0;
  154. section[image->num_sections].size = 0x0;
  155. section[image->num_sections].flags = 0;
  156. while (raw_bytes < raw_bytes_read)
  157. {
  158. u32 count;
  159. u32 address;
  160. u32 record_type;
  161. u32 checksum;
  162. if (sscanf(&buffer[raw_bytes], ":%2x%4x%2x", &count, &address, &record_type) != 3)
  163. {
  164. return ERROR_IMAGE_FORMAT_ERROR;
  165. }
  166. raw_bytes += 9;
  167. if (record_type == 0) /* Data Record */
  168. {
  169. if ((full_address & 0xffff) != address)
  170. {
  171. /* we encountered a nonconsecutive location, create a new section,
  172. * unless the current section has zero size, in which case this specifies
  173. * the current section's base address
  174. */
  175. if (section[image->num_sections].size != 0)
  176. {
  177. image->num_sections++;
  178. section[image->num_sections].size = 0x0;
  179. section[image->num_sections].flags = 0;
  180. section[image->num_sections].private = &ihex->buffer[cooked_bytes];
  181. }
  182. section[image->num_sections].base_address =
  183. (full_address & 0xffff0000) | address;
  184. full_address = (full_address & 0xffff0000) | address;
  185. }
  186. while (count-- > 0)
  187. {
  188. sscanf(&buffer[raw_bytes], "%2hhx", &ihex->buffer[cooked_bytes]);
  189. raw_bytes += 2;
  190. cooked_bytes += 1;
  191. section[image->num_sections].size += 1;
  192. full_address++;
  193. }
  194. }
  195. else if (record_type == 1) /* End of File Record */
  196. {
  197. /* finish the current section */
  198. image->num_sections++;
  199. /* copy section information */
  200. image->sections = malloc(sizeof(image_section_t) * image->num_sections);
  201. for (i = 0; i < image->num_sections; i++)
  202. {
  203. image->sections[i].private = section[i].private;
  204. image->sections[i].base_address = section[i].base_address +
  205. ((image->base_address_set) ? image->base_address : 0);
  206. image->sections[i].size = section[i].size;
  207. image->sections[i].flags = section[i].flags;
  208. }
  209. free(buffer);
  210. return ERROR_OK;
  211. }
  212. else if (record_type == 4) /* Extended Linear Address Record */
  213. {
  214. u16 upper_address;
  215. sscanf(&buffer[raw_bytes], "%4hx", &upper_address);
  216. raw_bytes += 4;
  217. if ((full_address >> 16) != upper_address)
  218. {
  219. /* we encountered a nonconsecutive location, create a new section,
  220. * unless the current section has zero size, in which case this specifies
  221. * the current section's base address
  222. */
  223. if (section[image->num_sections].size != 0)
  224. {
  225. image->num_sections++;
  226. section[image->num_sections].size = 0x0;
  227. section[image->num_sections].flags = 0;
  228. section[image->num_sections].private = &ihex->buffer[cooked_bytes];
  229. }
  230. section[image->num_sections].base_address =
  231. (full_address & 0xffff) | (upper_address << 16);
  232. full_address = (full_address & 0xffff) | (upper_address << 16);
  233. }
  234. }
  235. else if (record_type == 5) /* Start Linear Address Record */
  236. {
  237. u32 start_address;
  238. sscanf(&buffer[raw_bytes], "%8x", &start_address);
  239. raw_bytes += 8;
  240. image->start_address_set = 1;
  241. image->start_address = be_to_h_u32((u8*)&start_address);
  242. }
  243. else
  244. {
  245. free(buffer);
  246. ERROR("unhandled IHEX record type: %i", record_type);
  247. return ERROR_IMAGE_FORMAT_ERROR;
  248. }
  249. sscanf(&buffer[raw_bytes], "%2x", &checksum);
  250. raw_bytes += 2;
  251. /* consume new-line character(s) */
  252. if ((buffer[raw_bytes] == '\n') || (buffer[raw_bytes] == '\r'))
  253. raw_bytes++;
  254. if ((buffer[raw_bytes] == '\n') || (buffer[raw_bytes] == '\r'))
  255. raw_bytes++;
  256. }
  257. free(buffer);
  258. ERROR("premature end of IHEX file, no end-of-file record found");
  259. return ERROR_IMAGE_FORMAT_ERROR;
  260. }
  261. int image_elf_read_headers(image_t *image)
  262. {
  263. image_elf_t *elf = image->type_private;
  264. u32 read_bytes;
  265. u32 i,j;
  266. int retval;
  267. elf->header = malloc(sizeof(Elf32_Ehdr));
  268. if ((retval = fileio_read(&elf->fileio, sizeof(Elf32_Ehdr), (u8*)elf->header, &read_bytes)) != ERROR_OK)
  269. {
  270. ERROR("cannot read ELF file header, read failed");
  271. return ERROR_FILEIO_OPERATION_FAILED;
  272. }
  273. if (read_bytes != sizeof(Elf32_Ehdr))
  274. {
  275. ERROR("cannot read ELF file header, only partially read");
  276. return ERROR_FILEIO_OPERATION_FAILED;
  277. }
  278. if (strncmp((char*)elf->header->e_ident,ELFMAG,SELFMAG)!=0)
  279. {
  280. ERROR("invalid ELF file, bad magic number");
  281. return ERROR_IMAGE_FORMAT_ERROR;
  282. }
  283. if (elf->header->e_ident[EI_CLASS]!=ELFCLASS32)
  284. {
  285. ERROR("invalid ELF file, only 32bits files are supported");
  286. return ERROR_IMAGE_FORMAT_ERROR;
  287. }
  288. elf->endianness = elf->header->e_ident[EI_DATA];
  289. if ((elf->endianness!=ELFDATA2LSB)
  290. &&(elf->endianness!=ELFDATA2MSB))
  291. {
  292. ERROR("invalid ELF file, unknown endianess setting");
  293. return ERROR_IMAGE_FORMAT_ERROR;
  294. }
  295. elf->segment_count = field16(elf,elf->header->e_phnum);
  296. if (elf->segment_count==0)
  297. {
  298. ERROR("invalid ELF file, no program headers");
  299. return ERROR_IMAGE_FORMAT_ERROR;
  300. }
  301. elf->segments = malloc(elf->segment_count*sizeof(Elf32_Phdr));
  302. if ((retval = fileio_read(&elf->fileio, elf->segment_count*sizeof(Elf32_Phdr), (u8*)elf->segments, &read_bytes)) != ERROR_OK)
  303. {
  304. ERROR("cannot read ELF segment headers, read failed");
  305. return retval;
  306. }
  307. if (read_bytes != elf->segment_count*sizeof(Elf32_Phdr))
  308. {
  309. ERROR("cannot read ELF segment headers, only partially read");
  310. return ERROR_FILEIO_OPERATION_FAILED;
  311. }
  312. /* count useful segments (loadable) */
  313. image->num_sections = 0;
  314. for (i=0;i<elf->segment_count;i++)
  315. if (field32(elf,elf->segments[i].p_type) == PT_LOAD)
  316. image->num_sections++;
  317. /* alloc and fill sections array with loadable segments */
  318. image->sections = malloc(image->num_sections * sizeof(image_section_t));
  319. for (i=0,j=0;i<elf->segment_count;i++)
  320. {
  321. if (field32(elf,elf->segments[i].p_type) == PT_LOAD)
  322. {
  323. image->sections[j].size = field32(elf,elf->segments[i].p_memsz);
  324. image->sections[j].base_address = field32(elf,elf->segments[i].p_vaddr);
  325. image->sections[j].private = &elf->segments[i];
  326. image->sections[j].flags = field32(elf,elf->segments[i].p_flags);
  327. j++;
  328. }
  329. }
  330. image->start_address_set = 1;
  331. image->start_address = field32(elf,elf->header->e_entry);
  332. return ERROR_OK;
  333. }
  334. int image_elf_read_section(image_t *image, int section, u32 offset, u32 size, u8 *buffer, u32 *size_read)
  335. {
  336. image_elf_t *elf = image->type_private;
  337. Elf32_Phdr *segment = (Elf32_Phdr *)image->sections[section].private;
  338. u32 read_size,really_read;
  339. int retval;
  340. *size_read = 0;
  341. DEBUG("load segment %d at 0x%x (sz=0x%x)",section,offset,size);
  342. /* read initialized data in current segment if any */
  343. if (offset<field32(elf,segment->p_filesz))
  344. {
  345. /* maximal size present in file for the current segment */
  346. read_size = MIN(size, field32(elf,segment->p_filesz)-offset);
  347. DEBUG("read elf: size = 0x%x at 0x%x",read_size,
  348. field32(elf,segment->p_offset)+offset);
  349. /* read initialized area of the segment */
  350. if ((retval = fileio_seek(&elf->fileio, field32(elf,segment->p_offset)+offset)) != ERROR_OK)
  351. {
  352. ERROR("cannot find ELF segment content, seek failed");
  353. return retval;
  354. }
  355. if ((retval = fileio_read(&elf->fileio, read_size, buffer, &really_read)) != ERROR_OK)
  356. {
  357. ERROR("cannot read ELF segment content, read failed");
  358. return retval;
  359. }
  360. buffer += read_size;
  361. size -= read_size;
  362. offset += read_size;
  363. *size_read += read_size;
  364. /* need more data ? */
  365. if (!size)
  366. return ERROR_OK;
  367. }
  368. /* if there is remaining zeroed area in current segment */
  369. if (offset<field32(elf,segment->p_memsz))
  370. {
  371. /* fill zeroed part (BSS) of the segment */
  372. read_size = MIN(size, field32(elf,segment->p_memsz)-offset);
  373. DEBUG("zero fill: size = 0x%x",read_size);
  374. memset(buffer,0,read_size);
  375. *size_read += read_size;
  376. }
  377. return ERROR_OK;
  378. }
  379. int image_open(image_t *image, char *url, char *type_string)
  380. {
  381. int retval = ERROR_OK;
  382. if ((retval = identify_image_type(image, type_string, url)) != ERROR_OK)
  383. {
  384. return retval;
  385. }
  386. if (image->type == IMAGE_BINARY)
  387. {
  388. image_binary_t *image_binary;
  389. image_binary = image->type_private = malloc(sizeof(image_binary_t));
  390. if ((retval = fileio_open(&image_binary->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
  391. {
  392. strncpy(image->error_str, image_binary->fileio.error_str, IMAGE_MAX_ERROR_STRING);
  393. ERROR(image->error_str);
  394. return retval;
  395. }
  396. image->num_sections = 1;
  397. image->sections = malloc(sizeof(image_section_t));
  398. image->sections[0].base_address = 0x0;
  399. image->sections[0].size = image_binary->fileio.size;
  400. image->sections[0].flags = 0;
  401. if (image->base_address_set == 1)
  402. image->sections[0].base_address = image->base_address;
  403. return ERROR_OK;
  404. }
  405. else if (image->type == IMAGE_IHEX)
  406. {
  407. image_ihex_t *image_ihex;
  408. image_ihex = image->type_private = malloc(sizeof(image_ihex_t));
  409. if ((retval = fileio_open(&image_ihex->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
  410. {
  411. strncpy(image->error_str, image_ihex->fileio.error_str, IMAGE_MAX_ERROR_STRING);
  412. ERROR(image->error_str);
  413. return retval;
  414. }
  415. if ((retval = image_ihex_buffer_complete(image)) != ERROR_OK)
  416. {
  417. snprintf(image->error_str, IMAGE_MAX_ERROR_STRING,
  418. "failed buffering IHEX image, check daemon output for additional information");
  419. ERROR(image->error_str);
  420. fileio_close(&image_ihex->fileio);
  421. return retval;
  422. }
  423. }
  424. else if (image->type == IMAGE_ELF)
  425. {
  426. image_elf_t *image_elf;
  427. image_elf = image->type_private = malloc(sizeof(image_elf_t));
  428. if ((retval = fileio_open(&image_elf->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
  429. {
  430. strncpy(image->error_str, image_elf->fileio.error_str, IMAGE_MAX_ERROR_STRING);
  431. ERROR(image->error_str);
  432. return retval;
  433. }
  434. if ((retval = image_elf_read_headers(image)) != ERROR_OK)
  435. {
  436. snprintf(image->error_str, IMAGE_MAX_ERROR_STRING,
  437. "failed to read ELF headers, check daemon output for additional information");
  438. ERROR(image->error_str);
  439. fileio_close(&image_elf->fileio);
  440. return retval;
  441. }
  442. }
  443. else if (image->type == IMAGE_MEMORY)
  444. {
  445. image_memory_t *image_memory;
  446. image->num_sections = 1;
  447. image->sections = malloc(sizeof(image_section_t));
  448. image->sections[0].base_address = 0x0;
  449. image->sections[0].size = 0xffffffff;
  450. image->sections[0].flags = 0;
  451. image_memory = image->type_private = malloc(sizeof(image_memory_t));
  452. image_memory->target = get_target_by_num(strtoul(url, NULL, 0));;
  453. image_memory->cache = NULL;
  454. image_memory->cache_address = 0x0;
  455. }
  456. return retval;
  457. };
  458. int image_read_section(image_t *image, int section, u32 offset, u32 size, u8 *buffer, u32 *size_read)
  459. {
  460. int retval;
  461. if (image->type == IMAGE_BINARY)
  462. {
  463. image_binary_t *image_binary = image->type_private;
  464. /* only one section in a plain binary */
  465. if (section != 0)
  466. return ERROR_INVALID_ARGUMENTS;
  467. if ((offset > image->sections[0].size) || (offset + size > image->sections[0].size))
  468. return ERROR_INVALID_ARGUMENTS;
  469. /* seek to offset */
  470. if ((retval = fileio_seek(&image_binary->fileio, offset)) != ERROR_OK)
  471. {
  472. strncpy(image->error_str, image_binary->fileio.error_str, IMAGE_MAX_ERROR_STRING);
  473. return retval;
  474. }
  475. /* return requested bytes */
  476. if ((retval = fileio_read(&image_binary->fileio, size, buffer, size_read)) != ERROR_OK)
  477. {
  478. strncpy(image->error_str, image_binary->fileio.error_str, IMAGE_MAX_ERROR_STRING);
  479. return retval;
  480. }
  481. }
  482. else if (image->type == IMAGE_IHEX)
  483. {
  484. memcpy(buffer, (u8*)image->sections[section].private + offset, size);
  485. *size_read = size;
  486. image->error_str[0] = '\0';
  487. return ERROR_OK;
  488. }
  489. else if (image->type == IMAGE_ELF)
  490. {
  491. return image_elf_read_section(image, section, offset, size, buffer, size_read);
  492. }
  493. else if (image->type == IMAGE_MEMORY)
  494. {
  495. image_memory_t *image_memory = image->type_private;
  496. u32 address = image->sections[section].base_address + offset;
  497. *size_read = 0;
  498. while ((size - *size_read) > 0)
  499. {
  500. u32 size_in_cache;
  501. if (!image_memory->cache
  502. || (address < image_memory->cache_address)
  503. || (address >= (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE)))
  504. {
  505. if (!image_memory->cache)
  506. image_memory->cache = malloc(IMAGE_MEMORY_CACHE_SIZE);
  507. if (target_read_buffer(image_memory->target, address & ~(IMAGE_MEMORY_CACHE_SIZE - 1),
  508. IMAGE_MEMORY_CACHE_SIZE, image_memory->cache) != ERROR_OK)
  509. {
  510. free(image_memory->cache);
  511. return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
  512. }
  513. image_memory->cache_address = address & ~(IMAGE_MEMORY_CACHE_SIZE - 1);
  514. }
  515. size_in_cache = (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE) - address;
  516. memcpy(buffer + *size_read,
  517. image_memory->cache + (address - image_memory->cache_address),
  518. (size_in_cache > size) ? size : size_in_cache
  519. );
  520. *size_read += (size_in_cache > size) ? size : size_in_cache;
  521. address += (size_in_cache > size) ? size : size_in_cache;
  522. }
  523. }
  524. return ERROR_OK;
  525. }
  526. int image_close(image_t *image)
  527. {
  528. if (image->type == IMAGE_BINARY)
  529. {
  530. image_binary_t *image_binary = image->type_private;
  531. fileio_close(&image_binary->fileio);
  532. }
  533. else if (image->type == IMAGE_IHEX)
  534. {
  535. image_ihex_t *image_ihex = image->type_private;
  536. fileio_close(&image_ihex->fileio);
  537. if (image_ihex->buffer)
  538. free(image_ihex->buffer);
  539. }
  540. else if (image->type == IMAGE_ELF)
  541. {
  542. image_elf_t *image_elf = image->type_private;
  543. fileio_close(&image_elf->fileio);
  544. if (image_elf->header)
  545. free(image_elf->header);
  546. if (image_elf->segments)
  547. free(image_elf->segments);
  548. }
  549. else if (image->type == IMAGE_MEMORY)
  550. {
  551. image_memory_t *image_memory = image->type_private;
  552. if (image_memory->cache)
  553. free(image_memory->cache);
  554. }
  555. if (image->type_private)
  556. free(image->type_private);
  557. if (image->sections)
  558. free(image->sections);
  559. return ERROR_OK;
  560. }