|
| 1 | +#include <string.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <stdio.h> |
| 4 | +#include <unistd.h> |
| 5 | +#include <fcntl.h> |
| 6 | +#include <inttypes.h> |
| 7 | +#include <errno.h> |
| 8 | + |
| 9 | +#include <ihex2cyacd_cmdline.h> |
| 10 | + |
| 11 | +#define MAX_IHEX_FILE_LENGTH 512 |
| 12 | + |
| 13 | +#ifdef DEBUG |
| 14 | +#define dbg_printf(fmt, args...) printf(fmt, ## args) |
| 15 | +#else |
| 16 | +#define dbg_printf(fmt, args...) /* Don't do anything in release builds */ |
| 17 | +#endif |
| 18 | + |
| 19 | +struct cyacd_header_info { |
| 20 | + uint32_t silicon_id; |
| 21 | + uint8_t silicon_rev; |
| 22 | +}; |
| 23 | + |
| 24 | +static struct cyacd_header_info header_infos[] = |
| 25 | +{ |
| 26 | + [cpu_arg_CY8C41] = {0x04161193, 0x11}, |
| 27 | + [cpu_arg_CY8C42] = {0x04C81193, 0x11}, |
| 28 | +}; |
| 29 | + |
| 30 | +static struct ihex2cyacd_args_info args_info; |
| 31 | + |
| 32 | +#define get_line_chars(__str, __out_str, __n) \ |
| 33 | + __out_str[__n] = 0; \ |
| 34 | + memcpy(__out_str, __str, __n); \ |
| 35 | + __str += __n; |
| 36 | + |
| 37 | +static int parse_ihex_line(const char *line, uint8_t *length, uint16_t *addr, uint8_t *type, uint8_t data[MAX_IHEX_FILE_LENGTH]) |
| 38 | +{ |
| 39 | + char tmp_buf[MAX_IHEX_FILE_LENGTH]; |
| 40 | + int i; |
| 41 | + uint8_t crc, sum = 0; |
| 42 | + |
| 43 | + get_line_chars(line, tmp_buf, 2); |
| 44 | + *length = strtol(tmp_buf, NULL, 16); |
| 45 | + sum += *length; |
| 46 | + |
| 47 | + get_line_chars(line, tmp_buf, 4); |
| 48 | + *addr = strtol(tmp_buf, NULL, 16); |
| 49 | + sum += ((uint8_t) (*addr & 0xFF)); |
| 50 | + sum += ((uint8_t) ((*addr) >> 8 & 0xFF)); |
| 51 | + |
| 52 | + get_line_chars(line, tmp_buf, 2); |
| 53 | + *type = strtol(tmp_buf, NULL, 16); |
| 54 | + sum += *type; |
| 55 | + |
| 56 | + for( i = 0; i < *length; i++) { |
| 57 | + get_line_chars(line, tmp_buf, 2); |
| 58 | + data[i] = strtol(tmp_buf, NULL, 16); |
| 59 | + sum += data[i]; |
| 60 | + } |
| 61 | + |
| 62 | + get_line_chars(line, tmp_buf, 2); |
| 63 | + crc = strtol(tmp_buf, NULL, 16); |
| 64 | + |
| 65 | + if (crc != (~sum - 1)) { |
| 66 | + printf("CRC failed\n"); |
| 67 | + return 1; |
| 68 | + } |
| 69 | + |
| 70 | + return 0; |
| 71 | +} |
| 72 | + |
| 73 | +int main(int argc, char **argv) |
| 74 | +{ |
| 75 | + uint32_t bootloader_text_rows; |
| 76 | + char line_ptr[MAX_IHEX_FILE_LENGTH]; |
| 77 | + FILE *input_hex, *output_cyacd; |
| 78 | + struct cyacd_header_info *infos; |
| 79 | + size_t line_length = MAX_IHEX_FILE_LENGTH; |
| 80 | + |
| 81 | + if (ihex2cyacd_cmdline_parser(argc, argv, &args_info) != 0) { |
| 82 | + return EXIT_FAILURE; |
| 83 | + } |
| 84 | + |
| 85 | + input_hex = fopen(args_info.input_arg, "r"); |
| 86 | + if (!input_hex) { |
| 87 | + printf("Failed to open input file %s\n", args_info.input_arg); |
| 88 | + return 1; |
| 89 | + } |
| 90 | + output_cyacd = fopen(args_info.output_arg, "w+"); |
| 91 | + if (!output_cyacd) { |
| 92 | + printf("Failed to open output file %s\n", args_info.output_arg); |
| 93 | + return 1; |
| 94 | + } |
| 95 | + infos = &header_infos[args_info.cpu_arg]; |
| 96 | + |
| 97 | + bootloader_text_rows = args_info.bootloader_size_arg / args_info.flash_row_size_arg; |
| 98 | + |
| 99 | + fprintf(output_cyacd, "%08x%02x00\r\n", infos->silicon_id, infos->silicon_rev); |
| 100 | + |
| 101 | + while( getline(&line_ptr, &line_length, input_hex)) { |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + return 0; |
| 107 | +} |
0 commit comments