static int ihex_rs_iterate_data(ihex_recordset_t rs, ref UInt32 pIdx, /* ihex_record_t**rec*/ ref ihex_record_t pRec, ref UInt32 pOff) { UInt32 offset; ihex_record_t x; if (pIdx == 0 && pOff != 0) { pOff = 0; // force the offset to zero at start } for (; pIdx < rs.ihrs_count; ++pIdx) { x = (rs.ihrs_records[pIdx]); if (pRec != null) { pRec = x; //point the caller to current record } switch (x.ihr_type) { case ihex_rtype_t.IHEX_DATA: ++pIdx; //proceed to next record in next call return 0; //let the caller process the data case ihex_rtype_t.IHEX_EOF: if (pIdx < rs.ihrs_count - 1) { //IHEX_SET_ERROR_RETURN(IHEX_ERR_PREMATURE_EOF, "Premature EOF in record %i", p__i + 1); return (IHEX_ERR_PREMATURE_EOF); } else { //FIXME signal end of records pIdx = 0; //signal next index is 0 => we've finished if (pRec != null) { pRec = null; } return 0; } case ihex_rtype_t.IHEX_ESA: //offset = *(x.ihr_data) << 4; //????????????????????????????????? offset = x.ihr_data[0];// << 4; offset = offset << 4; if (pOff != null) { pOff = offset; } //#ifdef IHEX_DEBUG //printf("Switched offset to 0x%08x.\n", offset); //#endif break; case ihex_rtype_t.IHEX_ELA: offset = (UInt32)((x.ihr_data[0] << 24) + (x.ihr_data[1] << 16)); if (pOff != null) { pOff = offset; } //#ifdef IHEX_DEBUG //printf("Switched offset to 0x%08x.\n", offset); //#endif break; case ihex_rtype_t.IHEX_SSA: case ihex_rtype_t.IHEX_SLA: //skip body; next break; default: //IHEX_SET_ERROR_RETURN(IHEX_ERR_UNKNOWN_RECORD_TYPE, "Unknown record type in record %i: 0x%02x", p__i + 1, x.ihr_type); break; } } //IHEX_SET_ERROR_RETURN(IHEX_ERR_NO_EOF, "Missing EOF record"); return (IHEX_ERR_NO_EOF); }
public static int ihex_mem_copy_from_offset(ihex_recordset_t rs, ref Byte[] dst, UInt64 ofst, UInt64 byte_count, ihex_width_t w, ihex_byteorder_t o) { int r; UInt32 i, j, l; UInt32 offset = 0x00, address = 0x00; UInt32 start = 0x00, stop = 0x00; Byte[] d = (Byte[])dst; ihex_record_t x = new ihex_record_t(); if ((r = ihex_mem_zero(dst, byte_count)) != 0) { return r; } UInt32 target_idx; target_idx = 0; UInt32 bean_counter = 0; i = 0; // Start at first (0th) data record do { r = ihex_rs_iterate_data(rs, ref i, ref x, ref offset); if (r != 0) { return r; } else if (x == null) { break; } /* x.ihr_address; x.ihr_checksum; x.ihr_data; x.ihr_length; x.ihr_type; */ //x.ihr_type == ihex_rtype_t.IHEX_DATA; if (start == 0) { start = (uint)(offset + x.ihr_address + ofst); stop = (uint)(start + byte_count); } /**/ address = (offset + x.ihr_address); /* if (address >= byte_count) { //IHEX_SET_ERROR_RETURN(IHEX_ERR_ADDRESS_OUT_OF_RANGE, "Address 0x%08x is out of range", address); return(IHEX_ERR_ADDRESS_OUT_OF_RANGE); } */ //address = 0; Int32 _shift; UInt32 v; for (j = 0; j < x.ihr_length; j += (uint)w) { v = 0; for (l = 0; (l < (uint)w) && (j + l < x.ihr_length); l++) { _shift = (Int32)(8 * ((o == ihex_byteorder_t.IHEX_ORDER_BIGENDIAN) ? (((uint)w - 1) - l) : (l))); v += (UInt32)(((UInt32)(x.ihr_data[j + l])) << _shift); //v += x.ihr_data[j + l] << (8 * ((o == ihex_byteorder_t.IHEX_ORDER_BIGENDIAN) ? (((uint)w - 1) - l) : (l))); } if (((address + j) >= start) && ((address + j) < stop)) { if (w == ihex_width_t.IHEX_WIDTH_8BIT) { dst[target_idx + 0] = (Byte)((v >> 0) & 0x000000FF); bean_counter += 1; } if (w == ihex_width_t.IHEX_WIDTH_16BIT) { dst[target_idx + 0] = (Byte)((v >> 0) & 0x000000FF); dst[target_idx + 1] = (Byte)((v >> 8) & 0x000000FF); bean_counter += 2; } if (w == ihex_width_t.IHEX_WIDTH_32BIT) { dst[target_idx + 0] = (Byte)((v >> 0) & 0x000000FF); dst[target_idx + 1] = (Byte)((v >> 8) & 0x000000FF); dst[target_idx + 2] = (Byte)((v >> 16) & 0x000000FF); dst[target_idx + 3] = (Byte)((v >> 24) & 0x000000FF); bean_counter += 4; } //dst[target_idx] = v; target_idx += (uint)w; //#ifdef IHEX_DEBUG //printf("%08x . %08x = %08x\n", (address + j), v, *target); //#endif } if ((address + j) >= stop) break; } } while (i > 0); // i == 0 implies we have run out of data records to read Console.Write("bean_counter = 0x{0:x08}, {0}\n", bean_counter); return 0; }
//========== ihex_parse.c ========== //========== ihex_parse.c ========== //========== ihex_parse.c ========== //========== ihex_record.c ========== //========== ihex_record.c ========== //========== ihex_record.c ========== /* #define IHEX_SET_ERROR(errnum, error, ...) \ { char *e = (char*)malloc(512); \ snprintf(e, 512, error, ##__VA_ARGS__); \ ihex_set_error(errnum, e); } #define IHEX_SET_ERROR_RETURN(errnum, error, ...) \ { IHEX_SET_ERROR(errnum, error, ##__VA_ARGS__); \ return errnum; } */ public static UInt64 ihex_rs_get_size(ihex_recordset_t rs) { UInt64 s = 0; UInt32 i = 0; for (i = 0; i < rs.ihrs_count; i++) { s += rs.ihrs_records[i].ihr_length; } return s; }
static ihex_recordset_t ihex_rs_from_mem(Byte[] data, int size) { UInt32 i = 0; int r = 0, c = 0; //ihex_record_t rec; ihex_recordset_t recls; Byte l; ihex_last_errno = 0; ihex_last_error = ""; //----------------------------------------------------------------- // Count number of record marks in input string. c = 0; for (i = 0; i < size; i++) { if (data[i] == 0) // if we get a null that signifies the end break; if (data[i] == IHEX_CHR_RECORDMARK) // == ':') c++; } //Console.Write("ihex_rs_from_mem: number of record marks, c = {0}\n", c); //----------------------------------------------------------------- // Allocate the set of records // //rec = new ihex_record_t(); recls = new ihex_recordset_t(); recls.ihrs_count = (UInt32)c; recls.ihrs_records = new ihex_record_t[c]; for (i = 0; i < c; i++) { recls.ihrs_records[i] = new ihex_record_t(); recls.ihrs_records[i].ihr_address = 0; recls.ihrs_records[i].ihr_data = null; recls.ihrs_records[i].ihr_checksum = 0x00; recls.ihrs_records[i].ihr_length = 0; //recls.ihrs_records[i].ihr_type = ihex_rtype_t.IHEX_DATA; // whatever } /* // Allocate memory for the record container structure and for each // individual record. if ((rec = (ihex_record_t*) calloc(c, sizeof(ihex_record_t))) == NULL) { IHEX_SET_ERROR(IHEX_ERR_MALLOC_FAILED, "Could not allocate memory"); goto malloc_rec_failed; } if ((recls = (ihex_recordset_t*) malloc(sizeof(ihex_recordset_t))) == NULL) { IHEX_SET_ERROR(IHEX_ERR_MALLOC_FAILED, "Could not allocate memory"); goto malloc_recls_failed; } recls.ihrs_count = c; recls.ihrs_records = rec; */ //----------------------------------------------------------------- // Go through the data again and grab all the records int idx; idx = 0; i = 0; while ((idx < size) && (data[idx] != 0x00)) { i++; if (idx + 3 >= size) { break; } l = ihex_fromhex8(data, (idx + 1)); //(((Byte []) data) + 1); if (idx + 9 + l * 2 >= size || (r = ihex_parse_single_record(data, (UInt32)idx, l, recls.ihrs_records[i - 1])) != 0) { //TODO IHEX_SET_ERROR(r, "Line %i: %s", i, ihex_last_error); goto parse_single_failed; } /* if (i < 10) { //Console.Write("\n"); Console.Write("record {0}, Len = {1}, data[0..7] = {2:x02}, ", i - 1, recls.ihrs_records[i - 1].ihr_length, recls.ihrs_records[i - 1].ihr_data[0]); if (recls.ihrs_records[i - 1].ihr_data.Length > 1) Console.Write("{0:x02} ", recls.ihrs_records[i - 1].ihr_data[1]); if (recls.ihrs_records[i - 1].ihr_data.Length > 2) Console.Write("{0:x02} ", recls.ihrs_records[i - 1].ihr_data[2]); if (recls.ihrs_records[i - 1].ihr_data.Length > 3) Console.Write("{0:x02} ", recls.ihrs_records[i - 1].ihr_data[3]); if (recls.ihrs_records[i - 1].ihr_data.Length > 4) Console.Write("{0:x02} ", recls.ihrs_records[i - 1].ihr_data[4]); if (recls.ihrs_records[i - 1].ihr_data.Length > 5) Console.Write("{0:x02} ", recls.ihrs_records[i - 1].ihr_data[5]); if (recls.ihrs_records[i - 1].ihr_data.Length > 6) Console.Write("{0:x02} ", recls.ihrs_records[i - 1].ihr_data[6]); if (recls.ihrs_records[i - 1].ihr_data.Length > 7) Console.Write("{0:x02} ", recls.ihrs_records[i - 1].ihr_data[7]); Console.Write("\n"); } */ idx += (int)(recls.ihrs_records[i - 1].ihr_length * 2) + 10; while ((idx < size) && (data[idx] != IHEX_CHR_RECORDMARK) && (data[idx] != 0x00)) { idx++; //data ++; } } /* while (data < end && *(data) != 0x00) { i ++; if (data + 3 >= end) { break; } l = ihex_fromhex8(((Byte []) data) + 1); if (data + 9 + l * 2 >= end || (r = ihex_parse_single_record((Byte []) data, l, rec + i - 1)) != 0) { IHEX_SET_ERROR(r, "Line %i: %s", i, ihex_last_error); goto parse_single_failed; } data += (rec[i - 1].ihr_length * 2) + 10; while (data < end && *(data) != IHEX_CHR_RECORDMARK && *(data) != 0x00) { data ++; } } */ //----------------------------------------------------------------- // Finally check that the last record is the IHEX_EOF record. if (recls.ihrs_records[recls.ihrs_count - 1].ihr_type != ihex_rtype_t.IHEX_EOF) { //TODO IHEX_SET_ERROR(IHEX_ERR_NO_EOF, "Missing EOF record"); goto missing_eof_record; } return recls; missing_eof_record: //for (i = 0; i < recls.ihrs_count; i ++) //{ // free(recls.ihrs_records[i].ihr_data); //} parse_single_failed: //free(recls); //malloc_recls_failed: //free(rec); //malloc_rec_failed: return null; }
public static int ihex_rs_get_address_range(ihex_recordset_t rs, ref UInt32 pmin, ref UInt32 pmax) { int r; UInt32 offset = 0x00, address = 0x00;//, dummy_min, dummy_max; UInt32 min, max; UInt32 i = 0; ihex_record_t x = new ihex_record_t(); // Initialize range boundaries //if (pmin == null) //{ //min = dummy_min; //} //if (pmax == null) //{ //max = dummy_max; //} min = 0xFFFFFFFF; // UINT32_MAX; max = 0x00; int dd = 0; uint lasti = 0; do { lasti = i; r = ihex_rs_iterate_data(rs, ref i, ref x, ref offset); if (i == 0) { Console.Write("Last i = {0}, ", lasti); } if (r != 0) { return r; } else if (x == null) { break; } if (dd == 0) { Console.Write("First i = {0}, ", i); dd++; } address = (offset + x.ihr_address); if (address < min) { min = address; } /* if (address + x.ihr_length > max) { max = address + x.ihr_length; } */ if (address /*+ x.ihr_length*/ > max) { max = address + (x.ihr_length - 1); } } while (i > 0); if (pmin != null) pmin = min; if (pmax != null) pmax = max; return 0; }