Setting up repository
[linux-libre-firmware.git] / ath9k_htc / sboot / utility / patch_gen / main.c
1 /*
2  * Copyright (c) 2013 Qualcomm Atheros, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted (subject to the limitations in the
7  * disclaimer below) provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the
15  *    distribution.
16  *
17  *  * Neither the name of Qualcomm Atheros nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific prior written permission.
20  *
21  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
22  * GRANTED BY THIS LICENSE.  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
23  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
24  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
33  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 #include <stdio.h>
36 #include <time.h>
37 #include <string.h>
38 #include "dt_defs.h"
39 #include "patch.h"
40
41 #define SIZE_HASH_BUFFER        4096
42
43 #define ROM_SIZE        32
44
45 uint8_t pBuf[SIZE_HASH_BUFFER];
46
47 //unsigned short m_crc16ccitt;
48
49 void print_help()
50 {
51         printf("\n\rThis utility is used to prefix a header to the patch code!\n\r");
52         printf("For loading into the bootcode and the integrity crc header!\n\r"); 
53         printf("    - patch_hdr [patch binary] [load address]\n\r");
54         printf("\n\r   e.g. patch_hdr patch.bin 0x8e0000\n\r");
55 }
56
57
58 #define _ROMP_SIZE_     64
59
60 int main(int argc, char* argv[] )
61 {
62         FILE *out;
63         FILE *in;
64
65         struct rom_patch_st patch_item;
66         uint8_t *pBufPtr = NULL;
67         uint32_t ran_patch_size = 0;
68         uint8_t *ld_addr;
69         uint8_t *fun_addr;
70         uint8_t name_of_patch[32];
71
72         // init the patch 
73         _patch_init(&patch_item);
74
75         if(argc==2)
76         {
77                 _patch_oepn(pBuf, argv[1]);
78                 _patch_dump(pBuf);
79
80                 goto ERROR;
81         }
82         else if (argc<3)
83                 goto ERROR;
84         else
85                 printf("%s %s %s\n\r", argv[0], argv[1], argv[2]);
86
87
88
89         /***************************************************************/
90         // allocate the buffer space for func address
91         if( !(patch_item.fun = (uint8_t *)malloc(SIZE_HASH_BUFFER)) )
92                 goto ERROR;
93
94         // check the function address of the patch code
95         ld_addr = argv[2];
96         if( db_ascii_to_hex((ld_addr+2), &(patch_item.ld_addr))!=0 )
97                 goto ERROR;
98
99         // check the function address of the patch code
100         fun_addr = argv[3];
101         if( db_ascii_to_hex((fun_addr+2), &(patch_item.fun_addr))!=0 )
102                 goto ERROR;
103
104         /***************************************************************/
105
106         if( gen_patch_item(&patch_item, argv[1])!=TRUE )
107                 goto ERROR;
108
109         printf(" - prefix to the patch binary -\n\r");
110         _patch_dump(&patch_item);
111
112         sprintf(name_of_patch, "h_%s", argv[1]);
113
114         printf("%s is to be outputed\n\r", name_of_patch);
115
116         _patch_append(pBuf, &patch_item);
117
118         out = fopen(name_of_patch, "wb");
119
120         // ( struct size -4 ) + patch_code size + 4-%4( make it alignment ) 
121         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]);
122         fwrite((uint8_t *)pBuf, sizeof(uint8_t), (sizeof(struct rom_patch_st)-4)+patch_item.len+(4-(patch_item.len%4)), out);
123
124         fclose(out);
125
126         goto DONE;
127
128 ERROR:
129         print_help();
130
131 DONE:
132         if( patch_item.fun )
133                 free(patch_item.fun);
134
135         return 0;
136 }