BowlerKernel
IntelHexParser.java
Go to the documentation of this file.
1 
4 package com.neuronrobotics.sdk.bootloader;
5 
6 import java.io.IOException;
7 import java.util.ArrayList;
8 
9 import com.neuronrobotics.sdk.common.ByteList;
10 
11 // TODO: Auto-generated Javadoc
17 public class IntelHexParser {
18 
20  private long highAddress=0;
21 
23  ArrayList<ByteData> packetList = new ArrayList<ByteData>();
24 
26  private long dataIndex=0;
27 
29  private long base = 0x1D00A000L;
30 
32  private long head = 0x1D01FFFFL;
33 
40  public static String hex(long n) {
41  // call toUpperCase() if that's required
42  return String.format("0x%8s", Long.toHexString(n)).replace(' ', '0');
43  }
44 
51  private void checkAddressValidity(long currentAddress, NRBootCoreType type){
52  if(type==NRBootCoreType.PIC32){
53  if(currentAddress>head ){
54  throw new RuntimeException("Address "+hex(currentAddress)+" is larger than "+hex(head));
55  }
56  if(currentAddress<base){
57  throw new RuntimeException("Address "+hex(currentAddress)+" is less than "+hex(base));
58  }
59  }
60 
61  }
62 
70  public IntelHexParser(ArrayList<hexLine> lines, NRBootCoreType type) throws IOException{
71  ByteData tmp=null;
72  hexLine previousLine=null;
73  for (hexLine l : lines){
74  long currentAddress;
75  long endOfLastAddress = 0;
76 
77  if (l.getRecordType()==4){
78  byte[] haddr=l.getDataBytes();
79  highAddress = ByteList.convertToInt(haddr, false)*65536;
81  } if (l.getRecordType()==0){
82 
83  l.setHighAddress(highAddress);
85 
86  currentAddress=l.getStartAddress();
87  checkAddressValidity(currentAddress,type);
88  if (previousLine != null){
89  endOfLastAddress = previousLine.getEndAddress();
90  }
91  boolean isSequential = (currentAddress==(endOfLastAddress));
92  if(tmp==null){
93  tmp = new ByteData(currentAddress);
94  }
95  if(type==NRBootCoreType.PIC32){
96  if(!isSequential||(tmp.getData().length > 50)){
97  packetList.add(tmp);
98  tmp = new ByteData(currentAddress);
99  }
100  }
101  for (byte b : l.getDataBytes()){
102  if(type == NRBootCoreType.AVRxx4p){
103  if(!isSequential||(tmp.getData().length == 128)){
104  packetList.add(tmp);
105  tmp = new ByteData(currentAddress);
106  }
107  }
108  tmp.setData(b);
109 
110  currentAddress++;
111 
112  checkAddressValidity(currentAddress,type);
113  }
114  previousLine=l;
115  }
116 
117 
118  }
119  if (tmp.getData().length!=0){
120  packetList.add(tmp);
121  }
122 
123  }
124 
130  public int size(){
131  return packetList.size();
132  }
133 
139  public ByteData getNext(){
140  if (dataIndex < packetList.size()){
141  return packetList.get((int) dataIndex++);
142  }
143  return null;
144  }
145 
146 }
void checkAddressValidity(long currentAddress, NRBootCoreType type)
IntelHexParser(ArrayList< hexLine > lines, NRBootCoreType type)
static final int convertToInt(byte[] b)
Definition: ByteList.java:911