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; }
//--------------------------------------------------------------------- static int ihex_check_record(ihex_record_t r) { UInt32 i; //Byte t = 0; Int64 t = 0; t += r.ihr_length + ((r.ihr_address >> 8) & 0xFF) + (r.ihr_address & 0xFF) + (Byte)(r.ihr_type) + r.ihr_checksum; for (i = 0; i < r.ihr_length; i++) { t += r.ihr_data[i]; } return ((t & 0xFF) == 0) ? 0 : 1; }
//--------------------------------------------------------------------- //TODO static ihex_recordset_t ihex_rs_from_string(String data) //TODO { //TODO return ihex_rs_from_mem(data, strlen(data)); //TODO } //--------------------------------------------------------------------- static int ihex_parse_single_record(Byte[] data, UInt32 _ofst, UInt32 length, ihex_record_t record) { UInt32 i; // Records needs to begin with record mark (usually ":") if (data[_ofst] != IHEX_CHR_RECORDMARK) { //IHEX_SET_ERROR_RETURN(IHEX_ERR_PARSE_ERROR, "Missing record mark"); return (IHEX_ERR_PARSE_ERROR); } // Record layout: // 1 2 3 4 // 0 12 3456 78 90123456789012345678901234567890 12 // : 10 0100 00 214601360121470136007EFE09D21901 40 // +3 +7+9 +9+2*len| // 1 1 1 0123456789abcdef0123456789abcdef| // 1 4 8 V // :10800000E0570020F58001000F810100118101007F --karels record.ihr_length = (Byte)length; record.ihr_address = (UInt16)ihex_fromhex16(data, (int)(_ofst + 3)); record.ihr_type = (ihex_rtype_t)ihex_fromhex8(data, (int)(_ofst + 7)); record.ihr_checksum = (Byte)ihex_fromhex8(data, (int)(_ofst + 9 + record.ihr_length * 2)); record.ihr_data = new Byte[record.ihr_length]; /* if ((record.ihr_data = (Byte []) malloc(record.ihr_length)) == NULL) { //IHEX_SET_ERROR_RETURN(IHEX_ERR_MALLOC_FAILED, "Could not allocate memory"); return(IHEX_ERR_MALLOC_FAILED); } */ // Records needs to end with CRLF or LF. if ((data[_ofst + 11 + record.ihr_length * 2] != 0x0D || data[_ofst + 12 + record.ihr_length * 2] != 0x0A) && (data[_ofst + 11 + record.ihr_length * 2] != 0x0A)) { //free(record.ihr_data); //IHEX_SET_ERROR_RETURN(IHEX_ERR_WRONG_RECORD_LENGTH, "Incorrect record length"); return (IHEX_ERR_WRONG_RECORD_LENGTH); } for (i = 0; i < record.ihr_length; i++) { if (data[_ofst + 9 + i * 2] == 0x0A || data[_ofst + 9 + i * 2] == 0x0D) { //free(record.ihr_data); //IHEX_SET_ERROR_RETURN(IHEX_ERR_WRONG_RECORD_LENGTH, "Unexpected end of line"); return (IHEX_ERR_WRONG_RECORD_LENGTH); } record.ihr_data[i] = ihex_fromhex8(data, (int)(_ofst + 9 + i * 2)); } if (ihex_check_record(record) != 0) { //free(record.ihr_data); //IHEX_SET_ERROR_RETURN(IHEX_ERR_INCORRECT_CHECKSUM, "Checksum validation failed"); return (IHEX_ERR_INCORRECT_CHECKSUM); } return 0; }
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; }