]>
Commit | Line | Data |
---|---|---|
90836933 | 1 | #include <stdio.h> |
14ff7444 MG |
2 | #include <stdlib.h> |
3 | #include <strings.h> | |
4 | #include <string.h> | |
e433bc03 | 5 | #include <unistd.h> |
14ff7444 | 6 | #include <errno.h> |
af1fed3a | 7 | #include "rsb-crc.h" |
90836933 | 8 | #include "rsb-lz.h" |
e8563c43 | 9 | #include "filesystem.h" |
90836933 | 10 | |
2363a0d6 | 11 | void err_exit(const char *fname) |
14ff7444 | 12 | { |
e4a6d4c3 | 13 | fprintf(stderr,"%s: error extracting...\n", fname); |
14ff7444 MG |
14 | exit(1); |
15 | } | |
16 | ||
7e4dc833 | 17 | struct data_in_s { |
5e9ad31f MG |
18 | unsigned char *start; /* 0 */ |
19 | unsigned char *stop; /* 4 */ | |
7e4dc833 | 20 | unsigned char bit; /* 8 */ |
14ff7444 MG |
21 | unsigned char x; /* 9 */ |
22 | }; | |
23 | ||
7e4dc833 MG |
24 | struct data_out_s { |
25 | unsigned char *pos; | |
26 | unsigned char *end; | |
27 | }; | |
28 | ||
6b2c0993 | 29 | unsigned char get_next_in_byte(struct data_in_s *data_in) |
af1fed3a | 30 | { |
6b2c0993 | 31 | unsigned char byte; |
5e9ad31f | 32 | |
6b2c0993 | 33 | if (data_in->stop < data_in->start) |
2363a0d6 | 34 | err_exit(__func__); |
5e9ad31f | 35 | |
6b2c0993 MG |
36 | byte = *(data_in->start); |
37 | data_in->start++; | |
5e9ad31f | 38 | |
6b2c0993 | 39 | return byte; |
af1fed3a MG |
40 | } |
41 | ||
b0ddcea9 | 42 | unsigned char fn_59848(struct data_in_s *data_in) |
e4a6d4c3 | 43 | { |
b0ddcea9 | 44 | unsigned char r5; |
e4a6d4c3 | 45 | |
6b2c0993 MG |
46 | if (data_in->bit == 0x80) { |
47 | data_in->x = get_next_in_byte(data_in); | |
e4a6d4c3 | 48 | } |
6b2c0993 | 49 | |
b0ddcea9 | 50 | r5 = data_in->bit & data_in->x; |
6b2c0993 MG |
51 | |
52 | data_in->bit >>= 1; | |
53 | if (data_in->bit == 0) { | |
54 | data_in->bit = 0x80; | |
90c723bb MG |
55 | } |
56 | ||
e4a6d4c3 MG |
57 | if (r5 == 0) |
58 | return 0; | |
59 | ||
60 | return 1; | |
61 | } | |
62 | ||
7e4dc833 | 63 | unsigned int fn_598b4(struct data_in_s *r11_data, unsigned int r10_arg2) |
3772880c | 64 | { |
3772880c MG |
65 | unsigned int r6; |
66 | unsigned int r7; | |
67 | ||
b0ddcea9 | 68 | r6 = 1 << (r10_arg2 - 1); |
3772880c MG |
69 | |
70 | r7 = 0; | |
90c723bb | 71 | while (r6 != 0) { |
b0ddcea9 MG |
72 | if (r11_data->bit == 0x80) { |
73 | r11_data->x = get_next_in_byte(r11_data); | |
3772880c | 74 | } |
b0ddcea9 | 75 | if ((r11_data->bit & r11_data->x) != 0) |
3772880c MG |
76 | r7 = r7 | r6; |
77 | ||
78 | r6 = r6 >> 1; | |
79 | ||
b0ddcea9 | 80 | r11_data->bit >>= 1; |
3772880c | 81 | |
b0ddcea9 | 82 | if(r11_data->bit == 0) { |
7e4dc833 | 83 | r11_data->bit = 0x80; |
3772880c | 84 | } |
90c723bb | 85 | } |
3772880c MG |
86 | |
87 | return r7; | |
88 | } | |
89 | ||
b0ddcea9 | 90 | void write_byte(unsigned char byte, struct data_out_s *data_out) |
af1fed3a | 91 | { |
b0ddcea9 | 92 | if (data_out->pos > data_out->end) { |
2363a0d6 | 93 | err_exit(__func__); |
e4a6d4c3 | 94 | } |
9897bfc3 | 95 | |
b0ddcea9 MG |
96 | *(data_out->pos) = byte; |
97 | data_out->pos++; | |
af1fed3a MG |
98 | } |
99 | ||
7e4dc833 | 100 | void lz_expand(struct data_in_s *r10_data, struct data_out_s *data_out) |
af1fed3a MG |
101 | { |
102 | unsigned int r5; | |
103 | unsigned int r2; | |
90c723bb | 104 | unsigned char r4; |
05c92ac4 MG |
105 | unsigned int r6; |
106 | unsigned int r7; | |
107 | unsigned int r11; | |
6b2c0993 | 108 | unsigned char arr_59b64[1024]; |
af1fed3a MG |
109 | |
110 | r5 = 1; | |
111 | ||
90c723bb MG |
112 | while (1) { |
113 | while (1) { | |
114 | r2 = fn_59848(r10_data); | |
05c92ac4 MG |
115 | if (r2 == 0) |
116 | break; | |
af1fed3a | 117 | |
90c723bb | 118 | r2 = fn_598b4(r10_data, 8) & 0xff; |
af1fed3a | 119 | |
b0ddcea9 | 120 | write_byte(r2, data_out); |
6b2c0993 MG |
121 | arr_59b64[r5] = r2 & 0xff; |
122 | r5 = (r5 + 1) & 0x3ff; | |
90c723bb | 123 | } |
af1fed3a | 124 | |
90c723bb | 125 | r11 = fn_598b4(r10_data, 0x0a); |
05c92ac4 MG |
126 | if(r11 == 0) |
127 | return; | |
af1fed3a | 128 | |
90c723bb | 129 | r2 = fn_598b4(r10_data, 0x04); |
05c92ac4 MG |
130 | r7 = r2 + 1; |
131 | r6 = 0; | |
90c723bb | 132 | while (r6 <= r7) { |
6b2c0993 | 133 | r2 = (r6 + r11) & 0x3ff; |
05c92ac4 | 134 | r4 = arr_59b64[r2]; |
b0ddcea9 | 135 | write_byte(r4, data_out); |
90c723bb | 136 | arr_59b64[r5] = r4; |
6b2c0993 | 137 | r5 = (r5 + 1) & 0x3ff; |
05c92ac4 | 138 | r6++; |
90c723bb MG |
139 | } |
140 | } | |
af1fed3a MG |
141 | } |
142 | ||
6b2c0993 MG |
143 | /* Checksum is only used for the compressed firmware in 'firmware' */ |
144 | #if 0 | |
af1fed3a MG |
145 | unsigned int crc_check_59684(unsigned char *arg1, unsigned int arg2, unsigned int magic) |
146 | { | |
147 | unsigned int r3; | |
148 | unsigned int r4; | |
149 | unsigned int r5; | |
150 | ||
151 | #if 0 | |
152 | if (r0 < 0xc0000000) | |
153 | return 1; | |
154 | #endif | |
155 | ||
e433bc03 | 156 | /* ??? */ |
af1fed3a MG |
157 | r4 = *((unsigned int*)arg1 + 0x20); |
158 | r5 = *((unsigned int*)arg1 + 0x24); | |
159 | ||
e433bc03 | 160 | printf("magic: 0x%08x <-> 0x%08x\n", r5, magic); |
af1fed3a MG |
161 | if (r5 != magic) |
162 | return 2; | |
163 | ||
164 | if (arg2 >= r4) | |
165 | r5 = 0; | |
166 | else | |
167 | return 3; | |
168 | ||
169 | r5 = ~rsb_crc(~0x00, arg1, r4); | |
170 | r3 = *((unsigned int*)(arg1 + r4)); | |
171 | printf("Checksums: 0x%02x <-> 0x%02x\n", r5, r3); | |
172 | ||
173 | if (r3 == r5) | |
174 | return 0; | |
175 | ||
05c92ac4 | 176 | return 4; |
af1fed3a | 177 | } |
6b2c0993 | 178 | #endif |
af1fed3a | 179 | |
2363a0d6 | 180 | void extract_lz_file(unsigned char *inbuf, unsigned char *name) |
14ff7444 | 181 | { |
2363a0d6 MG |
182 | unsigned int len; |
183 | unsigned char *outbuf; | |
7e4dc833 MG |
184 | struct data_in_s data_in; |
185 | struct data_out_s data_out; | |
14ff7444 | 186 | |
2363a0d6 MG |
187 | if (*((unsigned int*)inbuf) != LZ_MAGIC) |
188 | err_exit(__func__); | |
e4a6d4c3 | 189 | |
2363a0d6 MG |
190 | len = *((unsigned int*)(inbuf + 4)); |
191 | printf(", length: %d", len); | |
14ff7444 | 192 | |
2363a0d6 | 193 | if ((outbuf = malloc(len)) == NULL) { |
e433bc03 MG |
194 | perror("malloc"); |
195 | exit(1); | |
196 | } | |
e433bc03 | 197 | |
2363a0d6 MG |
198 | bzero(outbuf, len); |
199 | ||
200 | data_in.start = inbuf + 8; | |
201 | data_in.stop = inbuf + len; | |
7e4dc833 MG |
202 | data_in.x = 0; |
203 | data_in.bit = 0x80; | |
14ff7444 | 204 | |
2363a0d6 MG |
205 | data_out.pos = outbuf; |
206 | data_out.end = outbuf + len; | |
14ff7444 | 207 | |
7e4dc833 | 208 | lz_expand(&data_in, &data_out); |
14ff7444 | 209 | |
e433bc03 | 210 | #if 0 |
6b2c0993 | 211 | /* Checksum is only used for the compressed firmware in 'firmware' */ |
af1fed3a MG |
212 | r3 = r7 + 0x20; |
213 | r5 = *((unsigned int*)r3); | |
214 | ||
e433bc03 MG |
215 | if ((ret = crc_check_59684(r7, r5, 0x46335053)) != 0) { |
216 | printf("crc_check return: %d\n", ret); | |
2363a0d6 | 217 | err_exit(__func__); |
e433bc03 MG |
218 | } |
219 | #endif | |
af1fed3a | 220 | |
2363a0d6 | 221 | write_file((char*)name, outbuf, len); |
e433bc03 | 222 | |
2363a0d6 | 223 | free(outbuf); |
14ff7444 | 224 | } |