Initial cut of the open ath9k htc firmware.
[open-ath9k-htc-firmware.git] / target_firmware / magpie_fw_dev / build / utility / patch_gen / main.c
1 #include <stdio.h>
2 #include <time.h>
3 #include <string.h>
4 #include "dt_defs.h"
5 #include "patch.h"
6
7 #define SIZE_HASH_BUFFER        4096
8
9 #define ROM_SIZE        32
10
11 uint8_t pBuf[SIZE_HASH_BUFFER];
12
13 //unsigned short m_crc16ccitt;
14
15 void print_help()
16 {
17         printf("\n\rThis utility is used to prefix a header to the patch code!\n\r");
18         printf("For loading into the bootcode and the integrity crc header!\n\r"); 
19         printf("    - patch_hdr [patch binary] [load address]\n\r");
20         printf("\n\r   e.g. patch_hdr patch.bin 0x8e0000\n\r");
21 }
22
23
24 #define _ROMP_SIZE_     64
25
26 int main(int argc, char* argv[] )
27 {
28         FILE *out;
29         FILE *in;
30
31         struct rom_patch_st patch_item;
32         uint8_t *pBufPtr = NULL;
33         uint32_t ran_patch_size = 0;
34         uint8_t *ld_addr;
35         uint8_t *fun_addr;
36         uint8_t name_of_patch[32];
37
38         // init the patch 
39         _patch_init(&patch_item);
40
41         if(argc==2)
42         {
43                 _patch_oepn(pBuf, argv[1]);
44                 _patch_dump(pBuf);
45
46                 goto ERROR;
47         }
48         else if (argc<3)
49                 goto ERROR;
50         else
51                 printf("%s %s %s\n\r", argv[0], argv[1], argv[2]);
52
53
54
55         /***************************************************************/
56         // allocate the buffer space for func address
57         if( !(patch_item.fun = (uint8_t *)malloc(SIZE_HASH_BUFFER)) )
58                 goto ERROR;
59
60         // check the function address of the patch code
61         ld_addr = argv[2];
62         if( db_ascii_to_hex((ld_addr+2), &(patch_item.ld_addr))!=0 )
63                 goto ERROR;
64
65         // check the function address of the patch code
66         fun_addr = argv[3];
67         if( db_ascii_to_hex((fun_addr+2), &(patch_item.fun_addr))!=0 )
68                 goto ERROR;
69
70         /***************************************************************/
71
72         if( gen_patch_item(&patch_item, argv[1])!=TRUE )
73                 goto ERROR;
74
75         printf(" - prefix to the patch binary -\n\r");
76         _patch_dump(&patch_item);
77
78         sprintf(name_of_patch, "h_%s", argv[1]);
79
80         printf("%s is to be outputed\n\r", name_of_patch);
81
82         _patch_append(pBuf, &patch_item);
83
84         out = fopen(name_of_patch, "wb");
85
86         // ( struct size -4 ) + patch_code size + 4-%4( make it alignment ) 
87         printf(" write %d bytes to h_%s as an output file", (sizeof(struct rom_patch_st)-4)+patch_item.len+(4-(patch_item.len%4)), argv[1]);
88         fwrite((uint8_t *)pBuf, sizeof(uint8_t), (sizeof(struct rom_patch_st)-4)+patch_item.len+(4-(patch_item.len%4)), out);
89
90         fclose(out);
91
92         goto DONE;
93
94 ERROR:
95         print_help();
96
97 DONE:
98         if( patch_item.fun )
99                 free(patch_item.fun);
100
101         return 0;
102 }