1 /* vi: set sw=4 ts=4: */
3 * Copyright (C) 2008 Michele Sanges <michele.sanges@gmail.com>
5 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 * - use kernel option 'vga=xxx' or otherwise enable framebuffer device.
9 * - put somewhere fbsplash.cfg file and an image in .ppm format.
10 * - run applet: $ setsid fbsplash [params] &
12 * -d /dev/fbN: framebuffer device (if not /dev/fb0)
13 * -s path_to_image_file (can be "-" for stdin)
15 * -f path_to_fifo (can be "-" for stdin)
16 * - if you want to run it only in presence of a kernel parameter
17 * (for example fbsplash=on), use:
18 * grep -q "fbsplash=on" </proc/cmdline && setsid fbsplash [params]
19 * - commands for fifo:
20 * "NN" (ASCII decimal number) - percentage to show on progress bar.
21 * "exit" (or just close fifo) - well you guessed it.
23 //config:config FBSPLASH
24 //config: bool "fbsplash (27 kb)"
26 //config: select PLATFORM_LINUX
28 //config: Shows splash image and progress bar on framebuffer device.
29 //config: Can be used during boot phase of an embedded device.
31 //config: - use kernel option 'vga=xxx' or otherwise enable fb device.
32 //config: - put somewhere fbsplash.cfg file and an image in .ppm format.
33 //config: - $ setsid fbsplash [params] &
34 //config: -c: hide cursor
35 //config: -d /dev/fbN: framebuffer device (if not /dev/fb0)
36 //config: -s path_to_image_file (can be "-" for stdin)
37 //config: -i path_to_cfg_file (can be "-" for stdin)
38 //config: -f path_to_fifo (can be "-" for stdin)
39 //config: - if you want to run it only in presence of kernel parameter:
40 //config: grep -q "fbsplash=on" </proc/cmdline && setsid fbsplash [params] &
41 //config: - commands for fifo:
42 //config: "NN" (ASCII decimal number) - percentage to show on progress bar
43 //config: "exit" - well you guessed it
45 //applet:IF_FBSPLASH(APPLET(fbsplash, BB_DIR_SBIN, BB_SUID_DROP))
47 //kbuild:lib-$(CONFIG_FBSPLASH) += fbsplash.o
49 //usage:#define fbsplash_trivial_usage
50 //usage: "-s IMGFILE [-c] [-d DEV] [-i INIFILE] [-f CMD]"
51 //usage:#define fbsplash_full_usage "\n\n"
53 //usage: "\n -c Hide cursor"
54 //usage: "\n -d Framebuffer device (default /dev/fb0)"
55 //usage: "\n -i Config file (var=value):"
56 //usage: "\n BAR_LEFT,BAR_TOP,BAR_WIDTH,BAR_HEIGHT"
57 //usage: "\n BAR_R,BAR_G,BAR_B"
58 //usage: "\n -f Control pipe (else exit after drawing image)"
59 //usage: "\n commands: 'NN' (% for progress bar) or 'exit'"
62 #include "common_bufsiz.h"
65 /* If you want logging messages on /tmp/fbsplash.log... */
70 bool bdebug_messages; // enable/disable logging
71 FILE *logfile_fd; // log file
73 unsigned char *addr; // pointer to framebuffer memory
74 unsigned ns[7]; // n-parameters
75 const char *image_filename;
76 struct fb_var_screeninfo scr_var;
77 struct fb_fix_screeninfo scr_fix;
78 unsigned bytes_per_pixel;
79 // cached (8 - scr_var.COLOR.length):
84 #define G (*ptr_to_globals)
85 #define INIT_G() do { \
86 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
89 #define nbar_width ns[0] // progress bar width
90 #define nbar_height ns[1] // progress bar height
91 #define nbar_posx ns[2] // progress bar horizontal position
92 #define nbar_posy ns[3] // progress bar vertical position
93 #define nbar_colr ns[4] // progress bar color red component
94 #define nbar_colg ns[5] // progress bar color green component
95 #define nbar_colb ns[6] // progress bar color blue component
98 #define DEBUG_MESSAGE(strMessage, args...) \
99 if (G.bdebug_messages) { \
100 fprintf(G.logfile_fd, "[%s][%s] - %s\n", \
101 __FILE__, __FUNCTION__, strMessage); \
104 #define DEBUG_MESSAGE(...) ((void)0)
108 * Configure palette for RGB:332
110 static void fb_setpal(int fd)
113 /* fb colors are 16 bit */
114 unsigned short red[256], green[256], blue[256];
118 for (i = 0; i < 256; i++) {
119 /* Color is encoded in pixel value as rrrgggbb.
120 * 3-bit color is mapped to 16-bit one as:
121 * 000 -> 00000000 00000000
122 * 001 -> 00100100 10010010
124 * 011 -> 01101101 10110110
125 * 100 -> 10010010 01001001
127 * 111 -> 11111111 11111111
129 red[i] = (( i >> 5 ) * 0x9249) >> 2; // rrr * 00 10010010 01001001 >> 2
130 green[i] = (((i >> 2) & 0x7) * 0x9249) >> 2; // ggg * 00 10010010 01001001 >> 2
131 /* 2-bit color is easier: */
132 blue[i] = ( i & 0x3) * 0x5555; // bb * 01010101 01010101
142 xioctl(fd, FBIOPUTCMAP, &cmap);
146 * Open and initialize the framebuffer device
147 * \param *strfb_device pointer to framebuffer device
149 static void fb_open(const char *strfb_device)
151 int fbfd = xopen(strfb_device, O_RDWR);
153 // framebuffer properties
154 xioctl(fbfd, FBIOGET_VSCREENINFO, &G.scr_var);
155 xioctl(fbfd, FBIOGET_FSCREENINFO, &G.scr_fix);
157 switch (G.scr_var.bits_per_pixel) {
168 bb_error_msg_and_die("unsupported %u bpp", (int)G.scr_var.bits_per_pixel);
172 G.red_shift = 8 - G.scr_var.red.length;
173 G.green_shift = 8 - G.scr_var.green.length;
174 G.blue_shift = 8 - G.scr_var.blue.length;
175 G.bytes_per_pixel = (G.scr_var.bits_per_pixel + 7) >> 3;
177 // map the device in memory
179 (G.scr_var.yres_virtual ?: G.scr_var.yres) * G.scr_fix.line_length,
180 PROT_WRITE, MAP_SHARED, fbfd, 0);
181 if (G.addr == MAP_FAILED)
182 bb_perror_msg_and_die("mmap");
184 // point to the start of the visible screen
185 G.addr += G.scr_var.yoffset * G.scr_fix.line_length + G.scr_var.xoffset * G.bytes_per_pixel;
191 * Return pixel value of the passed RGB color.
192 * This is performance critical fn.
194 static unsigned fb_pixel_value(unsigned r, unsigned g, unsigned b)
196 /* We assume that the r,g,b values are <= 255 */
198 if (G.bytes_per_pixel == 1) {
199 r = r & 0xe0; // 3-bit red
200 g = (g >> 3) & 0x1c; // 3-bit green
201 b = b >> 6; // 2-bit blue
204 if (G.bytes_per_pixel == 2) {
205 // ARM PL110 on Integrator/CP has RGBA5551 bit arrangement.
206 // We want to support bit locations like that.
208 // First shift out unused bits
209 r = r >> G.red_shift;
210 g = g >> G.green_shift;
211 b = b >> G.blue_shift;
212 // Then shift the remaining bits to their offset
213 return (r << G.scr_var.red.offset) +
214 (g << G.scr_var.green.offset) +
215 (b << G.scr_var.blue.offset);
218 return b + (g << 8) + (r << 16);
222 * Draw pixel on framebuffer
224 static void fb_write_pixel(unsigned char *addr, unsigned pixel)
226 switch (G.bytes_per_pixel) {
231 *(uint16_t *)addr = pixel;
234 *(uint32_t *)addr = pixel;
236 default: // 24 bits per pixel
238 addr[1] = pixel >> 8;
239 addr[2] = pixel >> 16;
245 * Draw hollow rectangle on framebuffer
247 static void fb_drawrectangle(void)
251 unsigned char *ptr1, *ptr2;
252 unsigned char nred = G.nbar_colr/2;
253 unsigned char ngreen = G.nbar_colg/2;
254 unsigned char nblue = G.nbar_colb/2;
256 thispix = fb_pixel_value(nred, ngreen, nblue);
259 ptr1 = G.addr + G.nbar_posy * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
260 ptr2 = G.addr + (G.nbar_posy + G.nbar_height - 1) * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
261 cnt = G.nbar_width - 1;
263 fb_write_pixel(ptr1, thispix);
264 fb_write_pixel(ptr2, thispix);
265 ptr1 += G.bytes_per_pixel;
266 ptr2 += G.bytes_per_pixel;
267 } while (--cnt >= 0);
270 ptr1 = G.addr + G.nbar_posy * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
271 ptr2 = G.addr + G.nbar_posy * G.scr_fix.line_length + (G.nbar_posx + G.nbar_width - 1) * G.bytes_per_pixel;
272 cnt = G.nbar_height - 1;
274 fb_write_pixel(ptr1, thispix);
275 fb_write_pixel(ptr2, thispix);
276 ptr1 += G.scr_fix.line_length;
277 ptr2 += G.scr_fix.line_length;
278 } while (--cnt >= 0);
283 * Draw filled rectangle on framebuffer
284 * \param nx1pos,ny1pos upper left position
285 * \param nx2pos,ny2pos down right position
286 * \param nred,ngreen,nblue rgb color
288 static void fb_drawfullrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos,
289 unsigned char nred, unsigned char ngreen, unsigned char nblue)
291 int cnt1, cnt2, nypos;
295 thispix = fb_pixel_value(nred, ngreen, nblue);
297 cnt1 = ny2pos - ny1pos;
300 ptr = G.addr + nypos * G.scr_fix.line_length + nx1pos * G.bytes_per_pixel;
301 cnt2 = nx2pos - nx1pos;
303 fb_write_pixel(ptr, thispix);
304 ptr += G.bytes_per_pixel;
305 } while (--cnt2 >= 0);
308 } while (--cnt1 >= 0);
313 * Draw a progress bar on framebuffer
314 * \param percent percentage of loading
316 static void fb_drawprogressbar(unsigned percent)
318 int left_x, top_y, pos_x;
319 unsigned width, height;
322 left_x = G.nbar_posx;
324 width = G.nbar_width - 1;
325 height = G.nbar_height - 1;
326 if ((int)(height | width) < 0)
328 // NB: "width" of 1 actually makes rect with width of 2!
331 // inner "empty" rectangle
336 if ((int)(height | width) < 0)
343 // actual progress bar
344 pos_x += (unsigned)(width * percent) / 100;
349 height++; // divide by 0 is bad
351 // draw one-line thick "rectangle"
352 // top line will have gray lvl 200, bottom one 100
353 unsigned gray_level = 100 + (unsigned)i*100 / height;
354 fb_drawfullrectangle(
356 gray_level, gray_level, gray_level);
362 fb_drawfullrectangle(
364 left_x + width, top_y + height,
365 G.nbar_colr, G.nbar_colg, G.nbar_colb);
370 * Draw image from PPM file
372 static void fb_drawimage(void)
376 unsigned char *pixline;
377 unsigned i, j, width, height, line_size;
379 if (LONE_DASH(G.image_filename)) {
382 int fd = open_zipped(G.image_filename, /*fail_if_not_compressed:*/ 0);
384 bb_simple_perror_msg_and_die(G.image_filename);
385 theme_file = xfdopen_for_read(fd);
389 * - Magic: two characters "P6".
390 * - Whitespace (blanks, TABs, CRs, LFs).
391 * - A width, formatted as ASCII characters in decimal.
393 * - A height, ASCII decimal.
395 * - The maximum color value, ASCII decimal, in 0..65535
396 * - Newline or other single whitespace character.
397 * (we support newline only)
398 * - A raster of Width * Height pixels in triplets of rgb
399 * in pure binary by 1 or 2 bytes. (we support only 1 byte)
401 #define concat_buf bb_common_bufsiz1
402 setup_common_bufsiz();
404 read_ptr = concat_buf;
406 int w, h, max_color_val;
407 int rem = concat_buf + COMMON_BUFSIZE - read_ptr;
409 || fgets(read_ptr, rem, theme_file) == NULL
411 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
413 read_ptr = strchrnul(read_ptr, '#');
414 *read_ptr = '\0'; /* ignore #comments */
415 if (sscanf(concat_buf, "P6 %u %u %u", &w, &h, &max_color_val) == 3
416 && max_color_val <= 255
418 width = w; /* w is on stack, width may be in register */
425 pixline = xmalloc(line_size);
427 if (width > G.scr_var.xres)
428 width = G.scr_var.xres;
429 if (height > G.scr_var.yres)
430 height = G.scr_var.yres;
431 for (j = 0; j < height; j++) {
432 unsigned char *pixel;
435 if (fread(pixline, 1, line_size, theme_file) != line_size)
436 bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
438 src = G.addr + j * G.scr_fix.line_length;
439 for (i = 0; i < width; i++) {
440 unsigned thispix = fb_pixel_value(pixel[0], pixel[1], pixel[2]);
441 fb_write_pixel(src, thispix);
442 src += G.bytes_per_pixel;
452 * Parse configuration file
453 * \param *cfg_filename name of the configuration file
455 static void init(const char *cfg_filename)
457 static const char param_names[] ALIGN1 =
458 "BAR_WIDTH\0" "BAR_HEIGHT\0"
459 "BAR_LEFT\0" "BAR_TOP\0"
460 "BAR_R\0" "BAR_G\0" "BAR_B\0"
466 parser_t *parser = config_open2(cfg_filename, xfopen_stdin);
467 while (config_read(parser, token, 2, 2, "#=",
468 (PARSE_NORMAL | PARSE_MIN_DIE) & ~(PARSE_TRIM | PARSE_COLLAPSE))) {
469 unsigned val = xatoi_positive(token[1]);
470 int i = index_in_strings(param_names, token[0]);
472 bb_error_msg_and_die("syntax error: %s", token[0]);
477 G.bdebug_messages = val;
478 if (G.bdebug_messages)
479 G.logfile_fd = xfopen_for_write("/tmp/fbsplash.log");
483 config_close(parser);
487 int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
488 int fbsplash_main(int argc UNUSED_PARAM, char **argv)
490 const char *fb_device, *cfg_filename, *fifo_filename;
491 FILE *fp = fp; // for compiler
498 // parse command line options
499 fb_device = "/dev/fb0";
501 fifo_filename = NULL;
502 bCursorOff = 1 & getopt32(argv, "cs:d:i:f:",
503 &G.image_filename, &fb_device, &cfg_filename, &fifo_filename);
505 // parse configuration file
509 // We must have -s IMG
510 if (!G.image_filename)
515 if (fifo_filename && bCursorOff) {
516 // hide cursor (BEFORE any fb ops)
517 full_write(STDOUT_FILENO, "\033[?25l", 6);
525 fp = xfopen_stdin(fifo_filename);
527 // For named pipes, we want to support this:
529 // fbsplash -f cmd_pipe .... &
534 // This means that we don't want fbsplash to get EOF
535 // when last writer closes input end.
536 // The simplest way is to open fifo for writing too
537 // and become an additional writer :)
538 open(fifo_filename, O_WRONLY); // errors are ignored
541 fb_drawprogressbar(0);
542 // Block on read, waiting for some input.
543 // Use of <stdio.h> style I/O allows to correctly
544 // handle a case when we have many buffered lines
545 // already in the pipe
546 while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
547 if (is_prefixed_with(num_buf, "exit")) {
548 DEBUG_MESSAGE("exit");
552 if (isdigit(num_buf[0]) && (num <= 100)) {
554 DEBUG_MESSAGE(itoa(num));
556 fb_drawprogressbar(num);
561 if (bCursorOff) // restore cursor
562 full_write(STDOUT_FILENO, "\033[?25h", 6);