Initial cut of the open ath9k htc firmware.
[open-ath9k-htc-firmware.git] / target_firmware / magpie_fw_dev / build / utility / patch_gen / patch.c
1 #include <stdio.h>
2 #include "dt_defs.h"
3 #include "patch.h"
4
5 //extern unsigned short m_crc16ccitt;
6 uint16_t uRead = 0;
7 uint8_t patchBuf[SIZE_HASH_BUFFER];
8
9
10 int db_ascii_to_hex(char* num_str, unsigned long* hex_num)
11 {
12     int i = 0;
13
14     *hex_num = 0;
15     while (num_str[i])
16     {
17         if ((num_str[i] >= '0') && (num_str[i] <= '9'))
18         {
19             *hex_num <<= 4;
20             *hex_num += (num_str[i] - '0');
21         }
22         else if ((num_str[i] >= 'A') && (num_str[i] <= 'F'))
23         {
24             *hex_num <<= 4;
25             *hex_num += (num_str[i] - 'A' + 10);
26         }
27         else if ((num_str[i] >= 'a') && (num_str[i] <= 'f'))
28         {
29             *hex_num <<= 4;
30             *hex_num += (num_str[i] - 'a' + 10);
31         }
32         else
33         {
34             return -1;
35         }
36         i++;
37     }
38     return 0;
39 }
40
41
42 #if 0
43 void dump_hex(uint8_t *buf, uint16_t size)
44 {
45         uint16_t i=0;
46
47
48         for(i=0; i<size; i++)
49         {
50                 if( (i%16==0) )
51                         printf("\n\r");
52                 else if( (i>0) && (i%8==0) )
53                         printf("- ");
54
55                 printf("%02x ", buf[i]);
56         }
57 }
58 #endif
59
60
61 BOOLEAN gen_patch_item(struct rom_patch_st *patch, uint8_t *file_name)
62 {
63         BOOLEAN retVal = FALSE;
64         FILE *in;
65         uint8_t *ptr;
66         uint16_t size = 0;
67
68         if((in = fopen(file_name,"rb")) != NULL)
69                 retVal = TRUE;
70         else
71                 goto ERR_DONE;
72
73         ptr = patchBuf;
74         while (1)
75         {
76                 ptr += uRead;
77                 uRead = fread(ptr, 1, SIZE_HASH_BUFFER, in);
78                 
79                 // debug to dump the data we read
80                 //dump_hex(ptr, uRead);
81
82                 patch->len += uRead;
83
84                 if(uRead != SIZE_HASH_BUFFER) 
85                         break;
86     }
87
88         memcpy(patch->fun, patchBuf, patch->len);
89         patch->crc16 = patch->len; // bugs? workaround?
90         
91         printf("\n\n");
92
93         fclose(in);
94 ERR_DONE:
95
96         return retVal;
97 }
98
99 BOOLEAN _patch_init(struct rom_patch_st *patch)
100 {
101         // init the pact_pack
102         //memset((uint8_t *)patch_patck, 0x0, sizeof(struct rom_patch_pack_st));
103         patch->crc16 = 0;
104         patch->len = 0;
105         patch->ld_addr = 0;
106         patch->fun_addr = 0;
107         patch->fun = NULL;
108
109         return TRUE;
110 }
111
112 void _patch_dump(struct rom_patch_st *patch)
113 {
114         printf(" -----------------------------\n\r");
115         printf(" patch code crc: 0x%04x\n\r", patch->crc16);
116         printf(" patch code size: %d\n\r", patch->len);
117         printf(" patch ld_addr: 0x%08x\n\r", patch->ld_addr);
118         printf(" patch fun_addr: 0x%08x\n\r", patch->fun_addr);
119         printf(" -----------------------------\n\r");
120 }
121
122
123 BOOLEAN _patch_oepn(uint8_t *buf, uint8_t *mFile)
124 {
125         FILE *in;
126         BOOLEAN retVal = FALSE;
127         uint16_t offset = 0;
128
129         if((in = fopen(mFile,"rb")) != NULL)
130         {
131                 printf("%s is opened successful!\n\r", mFile);
132                 retVal = TRUE;
133         }
134         else
135                 goto ERROR;
136
137         while (1)
138         {
139                 uRead = fread(buf+offset, 1, SIZE_HASH_BUFFER, in);
140
141                 // debug to dump the data we read
142                 //dump_hex(buf+offset, uRead);
143
144                 offset += uRead;
145                 if(uRead != SIZE_HASH_BUFFER) break;
146
147         }
148 ERROR:
149         if(in)
150                 close(in);
151
152         return retVal;
153 }
154
155 uint32_t _patch_append(uint8_t *buf, struct rom_patch_st *patch)
156 {
157
158         memcpy(buf, (uint8_t *)patch, (sizeof(struct rom_patch_st)-4));
159         memcpy(buf+(sizeof(struct rom_patch_st)-4), patch->fun, patch->len);
160
161         return 0;
162 }
163
164