Subversion Repositories HomeAutomation

Rev

Rev 75 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
75 johboh 1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.IO;
5
using System.Globalization;
6
 
7
namespace canBootloader
8
{
9
    public class HexFile
10
    {
257 arune 11
        private const ulong USER_RESET_ADDR = 0; //0x1000;  //0x1000 för PIC??
75 johboh 12
        private const ulong BUFFER_SIZE = 5200000;
13
        private const ulong LINE_MAX_SIZE = 1024;
14
        private const ulong ERROR_BUFFER_SIZE = 256;
15
        private const ulong FILE_PATH_SIZE = 256;
16
 
17
        private ulong length = 0;
18
        private ulong addrLower = 0;
19
        private ulong addrUpper = 0;
20
        private string filepath = "";
21
        private bool valid = false;
22
        private byte[] buff = new byte[BUFFER_SIZE];
23
 
24
        public HexFile() { }
25
 
26
        public bool isValid() { return valid; }
27
 
28
        public bool loadHex(string filepath)
29
        {
30
            // Populate buffer
31
 
32
            byte lineDataCount = 0;
33
            ulong lineAddrHigh = 0;
34
            ulong lineAddr = 0;
35
            byte lineCode = 0;
36
            ulong fullAddr = 0;
37
 
38
            this.filepath = filepath;
39
            this.length = 0;
40
            this.addrLower = 0x7FFFFFFF;
41
            this.addrUpper = 0;
42
            this.valid = false;
43
 
44
            // Prime the buffer
45
            for (ulong i = 0; i < BUFFER_SIZE; i++) buff[i] = 0xFF;
46
 
47
 
48
            // Try opening file
257 arune 49
            if (!File.Exists(this.filepath))
75 johboh 50
            {
51
                // The file could not be opened
52
                Console.WriteLine("Error in HexFile: Unable to load file.");
53
                return false;
54
            }
55
            else
56
            {
257 arune 57
                FileStream file = new FileStream(this.filepath, FileMode.Open, FileAccess.Read);
75 johboh 58
                StreamReader sr = new StreamReader(file);
59
 
60
                // Safely use the file stream
61
 
62
                // For each line, read until eof.
63
                while (!sr.EndOfStream)
64
                {
65
                    // read one line
66
                    string oneLine = sr.ReadLine();
67
 
68
                    // If valid line
69
                    if (!oneLine.Equals("") && oneLine[0]==':')
70
                    {
71
                        lineDataCount = byte.Parse(oneLine.Substring(1,2), NumberStyles.HexNumber);
72
                        lineAddr =  ulong.Parse(oneLine.Substring(3,4),NumberStyles.HexNumber) & 65535;    
73
                        lineCode = byte.Parse(oneLine.Substring(7,2), NumberStyles.HexNumber);  
74
 
75
                        fullAddr = (lineAddrHigh * 65536) + lineAddr;
76
 
77
                        switch(lineCode)
78
                        {
79
                                case 0:
80
                                    this.length +=lineDataCount;
81
                                    if ((fullAddr + lineDataCount - 1) > this.addrUpper) this.addrUpper = fullAddr + lineDataCount - 1;
82
                                    if (lineCode == 0 && fullAddr < this.addrLower) this.addrLower = fullAddr;
83
                                    for (int j = 0; j < lineDataCount; j++)
84
                                    {
85
                                        buff[fullAddr] = byte.Parse(oneLine.Substring(j * 2 + 9, 2), System.Globalization.NumberStyles.HexNumber);
86
                                        buff[fullAddr] &= (byte)255;
87
                                        fullAddr++;
88
                                    }
89
                                    break;
90
                                case 4:
91
                                    lineAddrHigh = ulong.Parse(oneLine.Substring(9, 4), System.Globalization.NumberStyles.HexNumber) & 65535;
92
                                    break;
257 arune 93
                                case 1: break;
75 johboh 94
 
95
                        }
96
 
97
                    }
98
 
99
                }
100
 
101
                this.valid = true;
102
                sr.Close();
103
            }
104
 
105
            // Force lower addr to start
106
            this.addrLower = USER_RESET_ADDR;
107
 
108
            return true;
109
        }
110
 
111
 
112
        public ulong getAddrUpper() { return addrUpper; }
113
 
114
        public ulong getAddrLower() { return addrLower; }
115
 
116
        public ulong getLength() { return length; }
117
 
118
        public byte getByte(ulong addr)
119
        {
120
            if (addr>=BUFFER_SIZE) return 0xFF;
121
            return buff[addr];
122
        }
123
 
124
    }
125
}