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#include "libbb.h"
62
63#define DEFAULTFBDEV FB_0
64#define DEFAULTFBMODE "/etc/fb.modes"
65
66
67#define FB_ACTIVATE_ALL 64
68enum {
69 FBIOGET_VSCREENINFO = 0x4600,
70 FBIOPUT_VSCREENINFO = 0x4601
71};
72
73struct fb_bitfield {
74 uint32_t offset;
75 uint32_t length;
76 uint32_t msb_right;
77};
78struct fb_var_screeninfo {
79 uint32_t xres;
80 uint32_t yres;
81 uint32_t xres_virtual;
82 uint32_t yres_virtual;
83 uint32_t xoffset;
84 uint32_t yoffset;
85
86 uint32_t bits_per_pixel;
87 uint32_t grayscale;
88
89 struct fb_bitfield red;
90 struct fb_bitfield green;
91 struct fb_bitfield blue;
92 struct fb_bitfield transp;
93
94 uint32_t nonstd;
95
96 uint32_t activate;
97
98 uint32_t height;
99 uint32_t width;
100
101 uint32_t accel_flags;
102
103
104 uint32_t pixclock;
105 uint32_t left_margin;
106 uint32_t right_margin;
107 uint32_t upper_margin;
108 uint32_t lower_margin;
109 uint32_t hsync_len;
110 uint32_t vsync_len;
111 uint32_t sync;
112 uint32_t vmode;
113 uint32_t reserved[6];
114};
115
116static void copy_if_gt0(uint32_t *src, uint32_t *dst, unsigned cnt)
117{
118 do {
119 if ((int32_t) *src > 0)
120 *dst = *src;
121 src++;
122 dst++;
123 } while (--cnt);
124}
125
126static NOINLINE void copy_changed_values(
127 struct fb_var_screeninfo *base,
128 struct fb_var_screeninfo *set)
129{
130
131
132
133
134 copy_if_gt0(&set->xres, &base->xres, 4);
135
136 if ((int32_t) set->bits_per_pixel > 0) base->bits_per_pixel = set->bits_per_pixel;
137
138
139
140
141
142
143
144
145
146
147
148 copy_if_gt0(&set->pixclock, &base->pixclock, 9);
149}
150
151
152enum {
153 CMD_FB = 1,
154 CMD_DB = 2,
155 CMD_GEOMETRY = 3,
156 CMD_TIMING = 4,
157 CMD_ACCEL = 5,
158 CMD_HSYNC = 6,
159 CMD_VSYNC = 7,
160 CMD_LACED = 8,
161 CMD_DOUBLE = 9,
162
163 CMD_ALL = 11,
164 CMD_INFO = 12,
165 CMD_SHOW = 13,
166 CMD_CHANGE = 14,
167
168#if ENABLE_FEATURE_FBSET_FANCY
169 CMD_XRES = 100,
170 CMD_YRES = 101,
171 CMD_VXRES = 102,
172 CMD_VYRES = 103,
173 CMD_DEPTH = 104,
174 CMD_MATCH = 105,
175 CMD_PIXCLOCK = 106,
176 CMD_LEFT = 107,
177 CMD_RIGHT = 108,
178 CMD_UPPER = 109,
179 CMD_LOWER = 110,
180 CMD_HSLEN = 111,
181 CMD_VSLEN = 112,
182 CMD_CSYNC = 113,
183 CMD_GSYNC = 114,
184 CMD_EXTSYNC = 115,
185 CMD_BCAST = 116,
186 CMD_RGBA = 117,
187 CMD_STEP = 118,
188 CMD_MOVE = 119,
189#endif
190};
191
192static const struct cmdoptions_t {
193 const char name[9];
194 const unsigned char param_count;
195 const unsigned char code;
196} g_cmdoptions[] ALIGN1 = {
197
198
199 { "fb" , 1, CMD_FB },
200 { "db" , 1, CMD_DB },
201 { "a" , 0, CMD_ALL },
202 { "i" , 0, CMD_INFO },
203 { "g" , 5, CMD_GEOMETRY },
204 { "t" , 7, CMD_TIMING },
205 { "accel" , 1, CMD_ACCEL },
206 { "hsync" , 1, CMD_HSYNC },
207 { "vsync" , 1, CMD_VSYNC },
208 { "laced" , 1, CMD_LACED },
209 { "double" , 1, CMD_DOUBLE },
210 { "show" , 0, CMD_SHOW },
211 { "s" , 0, CMD_SHOW },
212#if ENABLE_FEATURE_FBSET_FANCY
213 { "all" , 0, CMD_ALL },
214 { "xres" , 1, CMD_XRES },
215 { "yres" , 1, CMD_YRES },
216 { "vxres" , 1, CMD_VXRES },
217 { "vyres" , 1, CMD_VYRES },
218 { "depth" , 1, CMD_DEPTH },
219 { "match" , 0, CMD_MATCH },
220 { "geometry", 5, CMD_GEOMETRY },
221 { "pixclock", 1, CMD_PIXCLOCK },
222 { "left" , 1, CMD_LEFT },
223 { "right" , 1, CMD_RIGHT },
224 { "upper" , 1, CMD_UPPER },
225 { "lower" , 1, CMD_LOWER },
226 { "hslen" , 1, CMD_HSLEN },
227 { "vslen" , 1, CMD_VSLEN },
228 { "timings" , 7, CMD_TIMING },
229 { "csync" , 1, CMD_CSYNC },
230 { "gsync" , 1, CMD_GSYNC },
231 { "extsync" , 1, CMD_EXTSYNC },
232 { "bcast" , 1, CMD_BCAST },
233 { "rgba" , 1, CMD_RGBA },
234 { "step" , 1, CMD_STEP },
235 { "move" , 1, CMD_MOVE },
236#endif
237};
238
239
240enum {
241 FB_SYNC_HOR_HIGH_ACT = 1,
242 FB_SYNC_VERT_HIGH_ACT = 2,
243#if ENABLE_FEATURE_FBSET_READMODE
244 FB_VMODE_INTERLACED = 1,
245 FB_VMODE_DOUBLE = 2,
246 FB_SYNC_EXT = 4,
247 FB_SYNC_COMP_HIGH_ACT = 8,
248#endif
249};
250
251#if ENABLE_FEATURE_FBSET_READMODE
252static void ss(uint32_t *x, uint32_t flag, char *buf, const char *what)
253{
254 if (strcmp(buf, what) == 0)
255 *x &= ~flag;
256 else
257 *x |= flag;
258}
259
260
261
262
263
264
265
266
267
268
269
270static NOINLINE int read_mode_db(struct fb_var_screeninfo *base, const char *fn,
271 const char *mode)
272{
273 char *token[2], *p, *s;
274 parser_t *parser = config_open(fn);
275
276 while (config_read(parser, token, 2, 1, "# \t\r", PARSE_NORMAL)) {
277 if (strcmp(token[0], "mode") != 0 || !token[1])
278 continue;
279 p = strstr(token[1], mode);
280 if (!p)
281 continue;
282 s = p + strlen(mode);
283
284
285 if (((!*s || isspace(*s)) && '"' != s[-1])
286 || ('"' == *s && '"' == p[-1])
287 ) {
288
289 break;
290 }
291 }
292
293 if (!token[0])
294 return 0;
295
296 while (config_read(parser, token, 2, 1, "# \t", PARSE_NORMAL)) {
297 int i;
298
299
300 if (strcmp(token[0], "endmode") == 0) {
301
302 return 1;
303 }
304 p = token[1];
305 i = index_in_strings(
306 "geometry\0timings\0interlaced\0double\0vsync\0hsync\0csync\0extsync\0rgba\0",
307 token[0]);
308 switch (i) {
309 case 0:
310 if (sizeof(int) == sizeof(base->xres)) {
311 sscanf(p, "%d %d %d %d %d",
312 &base->xres, &base->yres,
313 &base->xres_virtual, &base->yres_virtual,
314 &base->bits_per_pixel);
315 } else {
316 int base_xres, base_yres;
317 int base_xres_virtual, base_yres_virtual;
318 int base_bits_per_pixel;
319 sscanf(p, "%d %d %d %d %d",
320 &base_xres, &base_yres,
321 &base_xres_virtual, &base_yres_virtual,
322 &base_bits_per_pixel);
323 base->xres = base_xres;
324 base->yres = base_yres;
325 base->xres_virtual = base_xres_virtual;
326 base->yres_virtual = base_yres_virtual;
327 base->bits_per_pixel = base_bits_per_pixel;
328 }
329
330 break;
331 case 1:
332 if (sizeof(int) == sizeof(base->xres)) {
333 sscanf(p, "%d %d %d %d %d %d %d",
334 &base->pixclock,
335 &base->left_margin, &base->right_margin,
336 &base->upper_margin, &base->lower_margin,
337 &base->hsync_len, &base->vsync_len);
338 } else {
339 int base_pixclock;
340 int base_left_margin, base_right_margin;
341 int base_upper_margin, base_lower_margin;
342 int base_hsync_len, base_vsync_len;
343 sscanf(p, "%d %d %d %d %d %d %d",
344 &base_pixclock,
345 &base_left_margin, &base_right_margin,
346 &base_upper_margin, &base_lower_margin,
347 &base_hsync_len, &base_vsync_len);
348 base->pixclock = base_pixclock;
349 base->left_margin = base_left_margin;
350 base->right_margin = base_right_margin;
351 base->upper_margin = base_upper_margin;
352 base->lower_margin = base_lower_margin;
353 base->hsync_len = base_hsync_len;
354 base->vsync_len = base_vsync_len;
355 }
356
357 break;
358 case 2:
359 case 3: {
360 static const uint32_t syncs[] = {FB_VMODE_INTERLACED, FB_VMODE_DOUBLE};
361 ss(&base->vmode, syncs[i-2], p, "false");
362
363 break;
364 }
365 case 4:
366 case 5:
367 case 6: {
368 static const uint32_t syncs[] = {FB_SYNC_VERT_HIGH_ACT, FB_SYNC_HOR_HIGH_ACT, FB_SYNC_COMP_HIGH_ACT};
369 ss(&base->sync, syncs[i-4], p, "low");
370
371 break;
372 }
373 case 7:
374 ss(&base->sync, FB_SYNC_EXT, p, "false");
375
376 break;
377 case 8: {
378 int red_offset, red_length;
379 int green_offset, green_length;
380 int blue_offset, blue_length;
381 int transp_offset, transp_length;
382
383 sscanf(p, "%d/%d,%d/%d,%d/%d,%d/%d",
384 &red_length, &red_offset,
385 &green_length, &green_offset,
386 &blue_length, &blue_offset,
387 &transp_length, &transp_offset);
388 base->red.offset = red_offset;
389 base->red.length = red_length;
390 base->red.msb_right = 0;
391 base->green.offset = green_offset;
392 base->green.length = green_length;
393 base->green.msb_right = 0;
394 base->blue.offset = blue_offset;
395 base->blue.length = blue_length;
396 base->blue.msb_right = 0;
397 base->transp.offset = transp_offset;
398 base->transp.length = transp_length;
399 base->transp.msb_right = 0;
400 }
401 }
402 }
403 return 0;
404}
405#endif
406
407static NOINLINE void showmode(struct fb_var_screeninfo *v)
408{
409 double drate = 0, hrate = 0, vrate = 0;
410
411 if (v->pixclock) {
412 drate = 1e12 / v->pixclock;
413 hrate = drate / (v->left_margin + v->xres + v->right_margin + v->hsync_len);
414 vrate = hrate / (v->upper_margin + v->yres + v->lower_margin + v->vsync_len);
415 }
416 printf("\nmode \"%ux%u-%u\"\n"
417#if ENABLE_FEATURE_FBSET_FANCY
418 "\t# D: %.3f MHz, H: %.3f kHz, V: %.3f Hz\n"
419#endif
420 "\tgeometry %u %u %u %u %u\n"
421 "\ttimings %u %u %u %u %u %u %u\n"
422 "\taccel %s\n"
423 "\trgba %u/%u,%u/%u,%u/%u,%u/%u\n"
424 "endmode\n\n",
425 v->xres, v->yres, (int) (vrate + 0.5),
426#if ENABLE_FEATURE_FBSET_FANCY
427 drate / 1e6, hrate / 1e3, vrate,
428#endif
429 v->xres, v->yres, v->xres_virtual, v->yres_virtual, v->bits_per_pixel,
430 v->pixclock, v->left_margin, v->right_margin, v->upper_margin, v->lower_margin,
431 v->hsync_len, v->vsync_len,
432 (v->accel_flags > 0 ? "true" : "false"),
433 v->red.length, v->red.offset, v->green.length, v->green.offset,
434 v->blue.length, v->blue.offset, v->transp.length, v->transp.offset);
435}
436
437int fbset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
438int fbset_main(int argc, char **argv)
439{
440 enum {
441 OPT_CHANGE = (1 << 0),
442 OPT_SHOW = (1 << 1),
443 OPT_READMODE = (1 << 2),
444 OPT_ALL = (1 << 3),
445 };
446 struct fb_var_screeninfo var_old, var_set;
447 int fh, i;
448 unsigned options = 0;
449
450 const char *fbdev = DEFAULTFBDEV;
451 IF_FEATURE_FBSET_READMODE(const char *modefile = DEFAULTFBMODE;)
452 char *thisarg;
453 char *mode = mode;
454
455 memset(&var_set, 0xff, sizeof(var_set));
456
457
458 argv++;
459 argc--;
460 for (; argc > 0 && (thisarg = *argv) != NULL; argc--, argv++) {
461 if (thisarg[0] != '-') {
462 if (!ENABLE_FEATURE_FBSET_READMODE || argc != 1)
463 bb_show_usage();
464 mode = thisarg;
465 options |= OPT_READMODE;
466 goto contin;
467 }
468 for (i = 0; i < ARRAY_SIZE(g_cmdoptions); i++) {
469 if (strcmp(thisarg + 1, g_cmdoptions[i].name) != 0)
470 continue;
471 if (argc <= g_cmdoptions[i].param_count)
472 bb_show_usage();
473
474 switch (g_cmdoptions[i].code) {
475 case CMD_FB:
476 fbdev = argv[1];
477 break;
478 case CMD_DB:
479 IF_FEATURE_FBSET_READMODE(modefile = argv[1];)
480 break;
481 case CMD_ALL:
482 options |= OPT_ALL;
483 break;
484 case CMD_SHOW:
485 options |= OPT_SHOW;
486 break;
487 case CMD_GEOMETRY:
488 var_set.xres = xatou32(argv[1]);
489 var_set.yres = xatou32(argv[2]);
490 var_set.xres_virtual = xatou32(argv[3]);
491 var_set.yres_virtual = xatou32(argv[4]);
492 var_set.bits_per_pixel = xatou32(argv[5]);
493 break;
494 case CMD_TIMING:
495 var_set.pixclock = xatou32(argv[1]);
496 var_set.left_margin = xatou32(argv[2]);
497 var_set.right_margin = xatou32(argv[3]);
498 var_set.upper_margin = xatou32(argv[4]);
499 var_set.lower_margin = xatou32(argv[5]);
500 var_set.hsync_len = xatou32(argv[6]);
501 var_set.vsync_len = xatou32(argv[7]);
502 break;
503 case CMD_ACCEL:
504 break;
505 case CMD_HSYNC:
506 var_set.sync |= FB_SYNC_HOR_HIGH_ACT;
507 break;
508 case CMD_VSYNC:
509 var_set.sync |= FB_SYNC_VERT_HIGH_ACT;
510 break;
511#if ENABLE_FEATURE_FBSET_FANCY
512 case CMD_XRES:
513 var_set.xres = xatou32(argv[1]);
514 break;
515 case CMD_YRES:
516 var_set.yres = xatou32(argv[1]);
517 break;
518 case CMD_DEPTH:
519 var_set.bits_per_pixel = xatou32(argv[1]);
520 break;
521 case CMD_PIXCLOCK:
522 var_set.pixclock = xatou32(argv[1]);
523 break;
524#endif
525 default:
526 bb_perror_msg_and_die("option '%s' not handled",
527 g_cmdoptions[i].name);
528 }
529 switch (g_cmdoptions[i].code) {
530 case CMD_FB:
531 case CMD_DB:
532 case CMD_ALL:
533 case CMD_SHOW:
534 break;
535 default:
536
537 options |= OPT_CHANGE;
538 }
539 argc -= g_cmdoptions[i].param_count;
540 argv += g_cmdoptions[i].param_count;
541 goto contin;
542 }
543 bb_show_usage();
544 contin: ;
545 }
546
547 fh = xopen(fbdev, O_RDONLY);
548 xioctl(fh, FBIOGET_VSCREENINFO, &var_old);
549
550 if (options & OPT_READMODE) {
551#if ENABLE_FEATURE_FBSET_READMODE
552 if (!read_mode_db(&var_old, modefile, mode)) {
553 bb_error_msg_and_die("unknown video mode '%s'", mode);
554 }
555 options |= OPT_CHANGE;
556#endif
557 }
558
559 if (options & OPT_CHANGE) {
560 copy_changed_values(&var_old, &var_set);
561 if (options & OPT_ALL)
562 var_old.activate = FB_ACTIVATE_ALL;
563 xioctl(fh, FBIOPUT_VSCREENINFO, &var_old);
564 }
565
566 if (options == 0 || (options & OPT_SHOW))
567 showmode(&var_old);
568
569 if (ENABLE_FEATURE_CLEAN_UP)
570 close(fh);
571
572 return EXIT_SUCCESS;
573}
574