-
Notifications
You must be signed in to change notification settings - Fork 9
/
logotool.c
298 lines (260 loc) · 8.96 KB
/
logotool.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* (C) Copyright 2021 Dorian Rudolph
*
* SPDX-License-Identifier: GPL-2.0+
*
* Based on work with copyright:
* * (C) Copyright 2020 Rockchip Electronics Co., Ltd
* *
* * SPDX-License-Identifier: GPL-2.0+
* * Author: Wenping Zhang <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <png.h>
#define LOGO_IMG_SIZE 0x1000000
#define SCREEN_WIDTH 1872
#define SCREEN_HEIGHT 1404
#define LOGO_SIZE SCREEN_WIDTH * SCREEN_HEIGHT / 2
#define LOGO_COUNT 10
#define COLOR_BITS 4
#define ASSERTF(a, fmt, ...) do { if(!(a)) { fprintf(stderr, fmt, ##__VA_ARGS__); exit(1); } } while(0)
enum type_logo {
EINK_LOGO_RESET = 0,
EINK_LOGO_UBOOT = 1 << 0,
EINK_LOGO_KERNEL = 1 << 1,
EINK_LOGO_CHARGING_0 = 1 << 2,
EINK_LOGO_CHARGING_1 = 1 << 3,
EINK_LOGO_CHARGING_2 = 1 << 4,
EINK_LOGO_CHARGING_3 = 1 << 5,
EINK_LOGO_CHARGING_4 = 1 << 6,
EINK_LOGO_CHARGING_5 = 1 << 7,
EINK_LOGO_CHARGING_LOWPOWER = 1 << 8,
EINK_LOGO_OFF = 1 << 9,
};
const enum type_logo USED_LOGOS[] = {
EINK_LOGO_UBOOT,
EINK_LOGO_KERNEL,
EINK_LOGO_CHARGING_0,
EINK_LOGO_CHARGING_1,
EINK_LOGO_CHARGING_2,
EINK_LOGO_CHARGING_3,
EINK_LOGO_CHARGING_4,
EINK_LOGO_CHARGING_5,
EINK_LOGO_CHARGING_LOWPOWER,
EINK_LOGO_OFF,
};
const char *logo_file_name(enum type_logo logo) {
switch (logo) {
case EINK_LOGO_RESET:
return "logo_reset.png";
case EINK_LOGO_UBOOT:
return "logo_uboot.png";
case EINK_LOGO_KERNEL:
return "logo_kernel.png";
case EINK_LOGO_CHARGING_0:
return "logo_charging_0.png";
case EINK_LOGO_CHARGING_1:
return "logo_charging_1.png";
case EINK_LOGO_CHARGING_2:
return "logo_charging_2.png";
case EINK_LOGO_CHARGING_3:
return "logo_charging_3.png";
case EINK_LOGO_CHARGING_4:
return "logo_charging_4.png";
case EINK_LOGO_CHARGING_5:
return "logo_charging_5.png";
case EINK_LOGO_CHARGING_LOWPOWER:
return "logo_charging_lowpower.png";
case EINK_LOGO_OFF:
return "logo_off.png";
default:
ASSERTF(0, "Invalid logo type 0x%x.\n", logo);
}
}
//logo partition Header, 64byte
struct logo_part_header {
char magic[4]; /* must be "RKEL" */
uint32_t total_size;
uint32_t screen_width;
uint32_t screen_height;
uint32_t logo_count;
char version[4];
uint32_t rsv[10];
} __attribute__((packed));
#define PART_MAGIC "RKEL"
#define VERSION "1.00"
// logo image header,32 byte
struct grayscale_header {
char magic[4]; /* must be "GR04" */
uint16_t x;
uint16_t y;
uint16_t w;
uint16_t h;
uint32_t logo_type;
uint32_t data_offset; /* image offset in byte */
uint32_t data_size; /* image size in byte */
uint32_t rsv[2];
} __attribute__((packed));
#define GRAYSCALE_MAGIC "GR04"
/*
* The start address of logo image in logo.img must be aligned
* in 512 bytes,so the header size must be times of 512 bytes.
* Here we fix the size to 512 bytes, so the count of logo image
* can only support up to 14.
*/
struct logo_info {
struct logo_part_header part_hdr;
struct grayscale_header img_hdr[14];
} __attribute__((packed));
void *read_file(const char *path, size_t *sz) {
FILE *fp = fopen(path, "rb");
ASSERTF(fp, "Error opening file \"%s\": %s\n", path, strerror(errno));
fseek(fp, 0, SEEK_END);
*sz = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *buf = malloc(*sz);
size_t r = fread(buf, 1, *sz, fp);
ASSERTF(r == *sz, "Could not read file \"%s\" completely.\n", path);
fclose(fp);
return buf;
}
void write_file(const char *path, const void *buf, size_t sz) {
FILE *fp = fopen(path, "wb");
ASSERTF(fp, "Error opening file \"%s\": %s\n", path, strerror(errno));
size_t w = fwrite(buf, 1, sz, fp);
ASSERTF(w == sz, "Could not read file \"%s\" completely.\n", path);
fclose(fp);
}
void write_png(char *path, uint32_t w, uint32_t h, unsigned char *buf) {
FILE *fp = fopen(path, "wb");
ASSERTF(fp, "Error opening file \"%s\" for writing: %s\n", path, strerror(errno));
png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
ASSERTF(png, "Failed to create PNG write struct.");
png_infop info = png_create_info_struct(png);
ASSERTF(png, "Failed to create info struct.");
if (setjmp(png_jmpbuf(png)))
ASSERTF(0, "Error while creating PNG.");
png_init_io(png, fp);
png_set_IHDR(
png,
info,
w, h,
COLOR_BITS,
PNG_COLOR_TYPE_GRAY,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT
);
png_write_info(png, info);
png_bytepp row_pointers = malloc(h * sizeof(png_bytep));
ASSERTF(row_pointers, "malloc failed\n");
for (int i = 0; i < h; ++i) {
row_pointers[i] = buf + i * w / 2;
}
png_set_packswap(png);
png_write_image(png, row_pointers);
png_write_end(png, NULL);
free(row_pointers);
fclose(fp);
png_destroy_write_struct(&png, &info);
}
void read_png(const char *path, uint32_t w, uint32_t h, unsigned char *buf) {
FILE *fp = fopen(path, "rb");
ASSERTF(fp, "Error opening file \"%s\" for reading: %s\n", path, strerror(errno));
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
ASSERTF(png, "Failed to create PNG read struct.");
png_infop info = png_create_info_struct(png);
ASSERTF(png, "Failed to create info struct.");
if (setjmp(png_jmpbuf(png)))
ASSERTF(0, "Error while creating PNG.");
png_init_io(png, fp);
png_read_info(png, info);
uint32_t width = png_get_image_width(png, info);
ASSERTF(width == w, "Expected width %u but got %u.\n", w, width);
uint32_t height = png_get_image_height(png, info);
ASSERTF(height == h, "Expected height %u but got %u.\n", h, height);
uint32_t color_type = png_get_color_type(png, info);
ASSERTF(color_type == PNG_COLOR_TYPE_GRAY, "Expected grayscale.\n");
uint32_t bit_depth = png_get_bit_depth(png, info);
ASSERTF(bit_depth == COLOR_BITS, "Expected %d bit color depth.\n", COLOR_BITS);
png_set_packswap(png);
png_read_update_info(png, info);
png_bytepp row_pointers = malloc(sizeof(png_bytep) * height);
uint32_t rowbytes = width / 2;
ASSERTF(png_get_rowbytes(png, info) == rowbytes, "Unexpected number of rowbytes\n");
for (int i = 0; i < height; i++) {
row_pointers[i] = buf + i * rowbytes;
}
png_read_image(png, row_pointers);
fclose(fp);
png_destroy_read_struct(&png, &info, NULL);
}
void read_logos(const char *logo_img_path, char *logos_path) {
size_t sz;
void *buf = read_file(logo_img_path, &sz);
ASSERTF(sz >= sizeof(struct logo_info), "logo.img is too small.\n");
struct logo_info *info = buf;
ASSERTF(memcmp(info->part_hdr.magic, PART_MAGIC, 4) == 0, "logo.img has wrong magic bytes.\n");
printf("found %u logos\n", info->part_hdr.logo_count);
for (int i = 0; i < info->part_hdr.logo_count; ++i) {
struct grayscale_header hdr = info->img_hdr[i];
ASSERTF(memcmp(hdr.magic, GRAYSCALE_MAGIC, 4) == 0, "logo %d has wrong magic bytes.\n", i);
ASSERTF(hdr.data_size + hdr.data_offset <= sz, "logo.img too small to read logo %d.\n", i);
ASSERTF(hdr.data_size * 2 == hdr.w * hdr.h, "Logo %d size does not match its width and height.\n", i);
char logo_path[256];
sprintf(logo_path, "%s/%s", logos_path, logo_file_name(hdr.logo_type));
printf("creating %s\n", logo_path);
write_png(logo_path, hdr.w, hdr.h, buf + hdr.data_offset);
}
free(buf);
}
void write_logos(const char *logo_img_path, char *logos_path) {
void *buf = calloc(LOGO_IMG_SIZE, 1);
struct logo_info *info = buf;
memcpy(info->part_hdr.magic, PART_MAGIC, 4);
memcpy(info->part_hdr.version, VERSION, 4);
info->part_hdr.logo_count = LOGO_COUNT;
info->part_hdr.screen_height = SCREEN_HEIGHT;
info->part_hdr.screen_width = SCREEN_WIDTH;
uint32_t logo_offset = LOGO_SIZE;
if (logo_offset & 0xff) logo_offset += 0x100 - (logo_offset & 0xff);
info->part_hdr.total_size = (LOGO_COUNT - 1) * logo_offset + LOGO_SIZE + sizeof(struct logo_info);
for (int i = 0; i < LOGO_COUNT; ++i) {
struct grayscale_header *hdr = info->img_hdr + i;
memcpy(hdr->magic, GRAYSCALE_MAGIC, 4);
hdr->x = 0;
hdr->y = 0;
hdr->w = SCREEN_WIDTH;
hdr->h = SCREEN_HEIGHT;
hdr->data_size = LOGO_SIZE;
hdr->data_offset = sizeof(struct logo_info) + i * logo_offset;
hdr->logo_type = USED_LOGOS[i];
char logo_path[256];
sprintf(logo_path, "%s/%s", logos_path, logo_file_name(hdr->logo_type));
printf("reading %s\n", logo_path);
read_png(logo_path, hdr->w, hdr->h, buf + hdr->data_offset);
}
write_file(logo_img_path, buf, LOGO_IMG_SIZE);
free(buf);
}
int main(int argc, char **argv) {
if (argc != 4) goto usage;
char mode = argv[1][0];
char *logo_img_path = argv[2];
char *logos_path = argv[3];
if (mode == 'r') {
read_logos(logo_img_path, logos_path);
} else if (mode == 'w') {
write_logos(logo_img_path, logos_path);
} else {
goto usage;
}
return 0;
usage:
fprintf(stderr, "Usage: %s (r|w) <logo.img> <logodir> # read/write logo.img from logos in logodir", *argv);
return 1;
}