3
0

fbsplash.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2008 Michele Sanges <michele.sanges@gmail.com>
  4. *
  5. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  6. *
  7. * Usage:
  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] &
  11. * -c: hide cursor
  12. * -d /dev/fbN: framebuffer device (if not /dev/fb0)
  13. * -s path_to_image_file (can be "-" for stdin)
  14. * -i path_to_cfg_file
  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.
  22. */
  23. //config:config FBSPLASH
  24. //config: bool "fbsplash (26 kb)"
  25. //config: default y
  26. //config: help
  27. //config: Shows splash image and progress bar on framebuffer device.
  28. //config: Can be used during boot phase of an embedded device.
  29. //config: Usage:
  30. //config: - use kernel option 'vga=xxx' or otherwise enable fb device.
  31. //config: - put somewhere fbsplash.cfg file and an image in .ppm format.
  32. //config: - $ setsid fbsplash [params] &
  33. //config: -c: hide cursor
  34. //config: -d /dev/fbN: framebuffer device (if not /dev/fb0)
  35. //config: -s path_to_image_file (can be "-" for stdin)
  36. //config: -i path_to_cfg_file (can be "-" for stdin)
  37. //config: -f path_to_fifo (can be "-" for stdin)
  38. //config: - if you want to run it only in presence of kernel parameter:
  39. //config: grep -q "fbsplash=on" </proc/cmdline && setsid fbsplash [params] &
  40. //config: - commands for fifo:
  41. //config: "NN" (ASCII decimal number) - percentage to show on progress bar
  42. //config: "exit" - well you guessed it
  43. //applet:IF_FBSPLASH(APPLET(fbsplash, BB_DIR_SBIN, BB_SUID_DROP))
  44. //kbuild:lib-$(CONFIG_FBSPLASH) += fbsplash.o
  45. //usage:#define fbsplash_trivial_usage
  46. //usage: "-s IMGFILE [-c] [-d DEV] [-i INIFILE] [-f CMD]"
  47. //usage:#define fbsplash_full_usage "\n\n"
  48. //usage: " -s Image"
  49. //usage: "\n -c Hide cursor"
  50. //usage: "\n -d Framebuffer device (default /dev/fb0)"
  51. //usage: "\n -i Config file (var=value):"
  52. //usage: "\n BAR_LEFT,BAR_TOP,BAR_WIDTH,BAR_HEIGHT"
  53. //usage: "\n BAR_R,BAR_G,BAR_B,IMG_LEFT,IMG_TOP"
  54. //usage: "\n -f Control pipe (else exit after drawing image)"
  55. //usage: "\n commands: 'NN' (% for progress bar) or 'exit'"
  56. #include "libbb.h"
  57. #include "common_bufsiz.h"
  58. #include <linux/fb.h>
  59. /* If you want logging messages on /tmp/fbsplash.log... */
  60. #define DEBUG 0
  61. #define ESC "\033"
  62. struct globals {
  63. #if DEBUG
  64. bool bdebug_messages; // enable/disable logging
  65. FILE *logfile_fd; // log file
  66. #endif
  67. unsigned char *addr; // pointer to framebuffer memory
  68. unsigned ns[9]; // n-parameters
  69. const char *image_filename;
  70. struct fb_var_screeninfo scr_var;
  71. struct fb_fix_screeninfo scr_fix;
  72. unsigned bytes_per_pixel;
  73. // cached (8 - scr_var.COLOR.length):
  74. unsigned red_shift;
  75. unsigned green_shift;
  76. unsigned blue_shift;
  77. };
  78. #define G (*ptr_to_globals)
  79. #define INIT_G() do { \
  80. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  81. } while (0)
  82. #define nbar_width ns[0] // progress bar width
  83. #define nbar_height ns[1] // progress bar height
  84. #define nbar_posx ns[2] // progress bar horizontal position
  85. #define nbar_posy ns[3] // progress bar vertical position
  86. #define nbar_colr ns[4] // progress bar color red component
  87. #define nbar_colg ns[5] // progress bar color green component
  88. #define nbar_colb ns[6] // progress bar color blue component
  89. #define img_posx ns[7] // image horizontal position
  90. #define img_posy ns[8] // image vertical position
  91. #if DEBUG
  92. #define DEBUG_MESSAGE(strMessage, args...) \
  93. if (G.bdebug_messages) { \
  94. fprintf(G.logfile_fd, "[%s][%s] - %s\n", \
  95. __FILE__, __FUNCTION__, strMessage); \
  96. }
  97. #else
  98. #define DEBUG_MESSAGE(...) ((void)0)
  99. #endif
  100. /**
  101. * Configure palette for RGB:332
  102. */
  103. static void fb_setpal(int fd)
  104. {
  105. struct fb_cmap cmap;
  106. /* fb colors are 16 bit */
  107. unsigned short red[256], green[256], blue[256];
  108. unsigned i;
  109. /* RGB:332 */
  110. for (i = 0; i < 256; i++) {
  111. /* Color is encoded in pixel value as rrrgggbb.
  112. * 3-bit color is mapped to 16-bit one as:
  113. * 000 -> 00000000 00000000
  114. * 001 -> 00100100 10010010
  115. * ...
  116. * 011 -> 01101101 10110110
  117. * 100 -> 10010010 01001001
  118. * ...
  119. * 111 -> 11111111 11111111
  120. */
  121. red[i] = (( i >> 5 ) * 0x9249) >> 2; // rrr * 00 10010010 01001001 >> 2
  122. green[i] = (((i >> 2) & 0x7) * 0x9249) >> 2; // ggg * 00 10010010 01001001 >> 2
  123. /* 2-bit color is easier: */
  124. blue[i] = ( i & 0x3) * 0x5555; // bb * 01010101 01010101
  125. }
  126. cmap.start = 0;
  127. cmap.len = 256;
  128. cmap.red = red;
  129. cmap.green = green;
  130. cmap.blue = blue;
  131. cmap.transp = 0;
  132. xioctl(fd, FBIOPUTCMAP, &cmap);
  133. }
  134. /**
  135. * Open and initialize the framebuffer device
  136. * \param *strfb_device pointer to framebuffer device
  137. */
  138. static void fb_open(const char *strfb_device)
  139. {
  140. int fbfd = xopen(strfb_device, O_RDWR);
  141. // framebuffer properties
  142. xioctl(fbfd, FBIOGET_VSCREENINFO, &G.scr_var);
  143. xioctl(fbfd, FBIOGET_FSCREENINFO, &G.scr_fix);
  144. switch (G.scr_var.bits_per_pixel) {
  145. case 8:
  146. fb_setpal(fbfd);
  147. break;
  148. case 16:
  149. case 24:
  150. case 32:
  151. break;
  152. default:
  153. bb_error_msg_and_die("unsupported %u bpp", (int)G.scr_var.bits_per_pixel);
  154. break;
  155. }
  156. G.red_shift = 8 - G.scr_var.red.length;
  157. G.green_shift = 8 - G.scr_var.green.length;
  158. G.blue_shift = 8 - G.scr_var.blue.length;
  159. G.bytes_per_pixel = (G.scr_var.bits_per_pixel + 7) >> 3;
  160. // map the device in memory
  161. G.addr = mmap(NULL,
  162. (G.scr_var.yres_virtual ?: G.scr_var.yres) * G.scr_fix.line_length,
  163. PROT_WRITE, MAP_SHARED, fbfd, 0);
  164. if (G.addr == MAP_FAILED)
  165. bb_simple_perror_msg_and_die("mmap");
  166. // point to the start of the visible screen
  167. G.addr += G.scr_var.yoffset * G.scr_fix.line_length + G.scr_var.xoffset * G.bytes_per_pixel;
  168. close(fbfd);
  169. }
  170. /**
  171. * Return pixel value of the passed RGB color.
  172. * This is performance critical fn.
  173. */
  174. static unsigned fb_pixel_value(unsigned r, unsigned g, unsigned b)
  175. {
  176. /* We assume that the r,g,b values are <= 255 */
  177. if (G.bytes_per_pixel == 1) {
  178. r = r & 0xe0; // 3-bit red
  179. g = (g >> 3) & 0x1c; // 3-bit green
  180. b = b >> 6; // 2-bit blue
  181. return r + g + b;
  182. }
  183. if (G.bytes_per_pixel == 2) {
  184. // ARM PL110 on Integrator/CP has RGBA5551 bit arrangement.
  185. // We want to support bit locations like that.
  186. //
  187. // First shift out unused bits
  188. r = r >> G.red_shift;
  189. g = g >> G.green_shift;
  190. b = b >> G.blue_shift;
  191. // Then shift the remaining bits to their offset
  192. return (r << G.scr_var.red.offset) +
  193. (g << G.scr_var.green.offset) +
  194. (b << G.scr_var.blue.offset);
  195. }
  196. // RGB 888
  197. return b + (g << 8) + (r << 16);
  198. }
  199. /**
  200. * Draw pixel on framebuffer
  201. */
  202. static void fb_write_pixel(unsigned char *addr, unsigned pixel)
  203. {
  204. switch (G.bytes_per_pixel) {
  205. case 1:
  206. *addr = pixel;
  207. break;
  208. case 2:
  209. *(uint16_t *)addr = pixel;
  210. break;
  211. case 4:
  212. *(uint32_t *)addr = pixel;
  213. break;
  214. default: // 24 bits per pixel
  215. addr[0] = pixel;
  216. addr[1] = pixel >> 8;
  217. addr[2] = pixel >> 16;
  218. }
  219. }
  220. /**
  221. * Draw hollow rectangle on framebuffer
  222. */
  223. static void fb_drawrectangle(void)
  224. {
  225. int cnt;
  226. unsigned thispix;
  227. unsigned char *ptr1, *ptr2;
  228. unsigned char nred = G.nbar_colr/2;
  229. unsigned char ngreen = G.nbar_colg/2;
  230. unsigned char nblue = G.nbar_colb/2;
  231. thispix = fb_pixel_value(nred, ngreen, nblue);
  232. // horizontal lines
  233. ptr1 = G.addr + G.nbar_posy * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
  234. ptr2 = G.addr + (G.nbar_posy + G.nbar_height - 1) * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
  235. cnt = G.nbar_width - 1;
  236. do {
  237. fb_write_pixel(ptr1, thispix);
  238. fb_write_pixel(ptr2, thispix);
  239. ptr1 += G.bytes_per_pixel;
  240. ptr2 += G.bytes_per_pixel;
  241. } while (--cnt >= 0);
  242. // vertical lines
  243. ptr1 = G.addr + G.nbar_posy * G.scr_fix.line_length + G.nbar_posx * G.bytes_per_pixel;
  244. ptr2 = G.addr + G.nbar_posy * G.scr_fix.line_length + (G.nbar_posx + G.nbar_width - 1) * G.bytes_per_pixel;
  245. cnt = G.nbar_height - 1;
  246. do {
  247. fb_write_pixel(ptr1, thispix);
  248. fb_write_pixel(ptr2, thispix);
  249. ptr1 += G.scr_fix.line_length;
  250. ptr2 += G.scr_fix.line_length;
  251. } while (--cnt >= 0);
  252. }
  253. /**
  254. * Draw filled rectangle on framebuffer
  255. * \param nx1pos,ny1pos upper left position
  256. * \param nx2pos,ny2pos down right position
  257. * \param nred,ngreen,nblue rgb color
  258. */
  259. static void fb_drawfullrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos,
  260. unsigned char nred, unsigned char ngreen, unsigned char nblue)
  261. {
  262. int cnt1, cnt2, nypos;
  263. unsigned thispix;
  264. unsigned char *ptr;
  265. thispix = fb_pixel_value(nred, ngreen, nblue);
  266. cnt1 = ny2pos - ny1pos;
  267. nypos = ny1pos;
  268. do {
  269. ptr = G.addr + nypos * G.scr_fix.line_length + nx1pos * G.bytes_per_pixel;
  270. cnt2 = nx2pos - nx1pos;
  271. do {
  272. fb_write_pixel(ptr, thispix);
  273. ptr += G.bytes_per_pixel;
  274. } while (--cnt2 >= 0);
  275. nypos++;
  276. } while (--cnt1 >= 0);
  277. }
  278. /**
  279. * Draw a progress bar on framebuffer
  280. * \param percent percentage of loading
  281. */
  282. static void fb_drawprogressbar(unsigned percent)
  283. {
  284. int left_x, top_y, pos_x;
  285. unsigned width, height;
  286. // outer box
  287. left_x = G.nbar_posx;
  288. top_y = G.nbar_posy;
  289. width = G.nbar_width - 1;
  290. height = G.nbar_height - 1;
  291. if ((int)(height | width) < 0)
  292. return;
  293. // NB: "width" of 1 actually makes rect with width of 2!
  294. fb_drawrectangle();
  295. // inner "empty" rectangle
  296. left_x++;
  297. top_y++;
  298. width -= 2;
  299. height -= 2;
  300. if ((int)(height | width) < 0)
  301. return;
  302. pos_x = left_x;
  303. if (percent > 0) {
  304. int i, y;
  305. // actual progress bar
  306. pos_x += (unsigned)(width * percent) / 100;
  307. y = top_y;
  308. i = height;
  309. if (height == 0)
  310. height++; // divide by 0 is bad
  311. while (i >= 0) {
  312. // draw one-line thick "rectangle"
  313. // top line will have gray lvl 200, bottom one 100
  314. unsigned gray_level = 100 + (unsigned)i*100 / height;
  315. fb_drawfullrectangle(
  316. left_x, y, pos_x, y,
  317. gray_level, gray_level, gray_level);
  318. y++;
  319. i--;
  320. }
  321. }
  322. fb_drawfullrectangle(
  323. pos_x, top_y,
  324. left_x + width, top_y + height,
  325. G.nbar_colr, G.nbar_colg, G.nbar_colb);
  326. }
  327. /**
  328. * Draw image from PPM file
  329. */
  330. static void fb_drawimage(void)
  331. {
  332. FILE *theme_file;
  333. char *read_ptr;
  334. unsigned char *pixline;
  335. unsigned i, j, width, height, line_size;
  336. if (LONE_DASH(G.image_filename)) {
  337. theme_file = stdin;
  338. } else {
  339. int fd = open_zipped(G.image_filename, /*fail_if_not_compressed:*/ 0);
  340. if (fd < 0)
  341. bb_simple_perror_msg_and_die(G.image_filename);
  342. theme_file = xfdopen_for_read(fd);
  343. }
  344. /* Parse ppm header:
  345. * - Magic: two characters "P6".
  346. * - Whitespace (blanks, TABs, CRs, LFs).
  347. * - A width, formatted as ASCII characters in decimal.
  348. * - Whitespace.
  349. * - A height, ASCII decimal.
  350. * - Whitespace.
  351. * - The maximum color value, ASCII decimal, in 0..65535
  352. * - Newline or other single whitespace character.
  353. * (we support newline only)
  354. * - A raster of Width * Height pixels in triplets of rgb
  355. * in pure binary by 1 or 2 bytes. (we support only 1 byte)
  356. */
  357. #define concat_buf bb_common_bufsiz1
  358. setup_common_bufsiz();
  359. read_ptr = concat_buf;
  360. while (1) {
  361. int w, h, max_color_val;
  362. int rem = concat_buf + COMMON_BUFSIZE - read_ptr;
  363. if (rem < 2
  364. || fgets(read_ptr, rem, theme_file) == NULL
  365. ) {
  366. bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
  367. }
  368. read_ptr = strchrnul(read_ptr, '#');
  369. *read_ptr = '\0'; /* ignore #comments */
  370. if (sscanf(concat_buf, "P6 %u %u %u", &w, &h, &max_color_val) == 3
  371. && max_color_val <= 255
  372. ) {
  373. width = w; /* w is on stack, width may be in register */
  374. height = h;
  375. break;
  376. }
  377. }
  378. line_size = width*3;
  379. pixline = xmalloc(line_size);
  380. if ((width + G.img_posx) > G.scr_var.xres)
  381. width = G.scr_var.xres - G.img_posx;
  382. if ((height + G.img_posy) > G.scr_var.yres)
  383. height = G.scr_var.yres - G.img_posy;
  384. for (j = 0; j < height; j++) {
  385. unsigned char *pixel;
  386. unsigned char *src;
  387. if (fread(pixline, 1, line_size, theme_file) != line_size)
  388. bb_error_msg_and_die("bad PPM file '%s'", G.image_filename);
  389. pixel = pixline;
  390. src = G.addr + (G.img_posy + j) * G.scr_fix.line_length + G.img_posx * G.bytes_per_pixel;
  391. for (i = 0; i < width; i++) {
  392. unsigned thispix = fb_pixel_value(pixel[0], pixel[1], pixel[2]);
  393. fb_write_pixel(src, thispix);
  394. src += G.bytes_per_pixel;
  395. pixel += 3;
  396. }
  397. }
  398. free(pixline);
  399. fclose(theme_file);
  400. }
  401. /**
  402. * Parse configuration file
  403. * \param *cfg_filename name of the configuration file
  404. */
  405. static void init(const char *cfg_filename)
  406. {
  407. static const char param_names[] ALIGN1 =
  408. "BAR_WIDTH\0" "BAR_HEIGHT\0"
  409. "BAR_LEFT\0" "BAR_TOP\0"
  410. "BAR_R\0" "BAR_G\0" "BAR_B\0"
  411. "IMG_LEFT\0" "IMG_TOP\0"
  412. #if DEBUG
  413. "DEBUG\0"
  414. #endif
  415. ;
  416. char *token[2];
  417. parser_t *parser = config_open2(cfg_filename, xfopen_stdin);
  418. while (config_read(parser, token, 2, 2, "#=",
  419. (PARSE_NORMAL | PARSE_MIN_DIE) & ~(PARSE_TRIM | PARSE_COLLAPSE))) {
  420. unsigned val = xatoi_positive(token[1]);
  421. int i = index_in_strings(param_names, token[0]);
  422. if (i < 0)
  423. bb_error_msg_and_die("syntax error: %s", token[0]);
  424. if (i >= 0 && i < 9)
  425. G.ns[i] = val;
  426. #if DEBUG
  427. if (i == 9) {
  428. G.bdebug_messages = val;
  429. if (G.bdebug_messages)
  430. G.logfile_fd = xfopen_for_write("/tmp/fbsplash.log");
  431. }
  432. #endif
  433. }
  434. config_close(parser);
  435. }
  436. int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  437. int fbsplash_main(int argc UNUSED_PARAM, char **argv)
  438. {
  439. const char *fb_device, *cfg_filename, *fifo_filename;
  440. FILE *fp = fp; // for compiler
  441. char *num_buf;
  442. unsigned num;
  443. bool bCursorOff;
  444. INIT_G();
  445. // parse command line options
  446. fb_device = "/dev/fb0";
  447. cfg_filename = NULL;
  448. fifo_filename = NULL;
  449. bCursorOff = 1 & getopt32(argv, "cs:d:i:f:",
  450. &G.image_filename, &fb_device, &cfg_filename, &fifo_filename);
  451. // parse configuration file
  452. if (cfg_filename)
  453. init(cfg_filename);
  454. // We must have -s IMG
  455. if (!G.image_filename)
  456. bb_show_usage();
  457. fb_open(fb_device);
  458. if (fifo_filename && bCursorOff) {
  459. // hide cursor (BEFORE any fb ops)
  460. full_write(STDOUT_FILENO, ESC"[?25l", 6);
  461. }
  462. fb_drawimage();
  463. if (!fifo_filename)
  464. return EXIT_SUCCESS;
  465. fp = xfopen_stdin(fifo_filename);
  466. if (fp != stdin) {
  467. // For named pipes, we want to support this:
  468. // mkfifo cmd_pipe
  469. // fbsplash -f cmd_pipe .... &
  470. // ...
  471. // echo 33 >cmd_pipe
  472. // ...
  473. // echo 66 >cmd_pipe
  474. // This means that we don't want fbsplash to get EOF
  475. // when last writer closes input end.
  476. // The simplest way is to open fifo for writing too
  477. // and become an additional writer :)
  478. open(fifo_filename, O_WRONLY); // errors are ignored
  479. }
  480. fb_drawprogressbar(0);
  481. // Block on read, waiting for some input.
  482. // Use of <stdio.h> style I/O allows to correctly
  483. // handle a case when we have many buffered lines
  484. // already in the pipe
  485. while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
  486. if (is_prefixed_with(num_buf, "exit")) {
  487. DEBUG_MESSAGE("exit");
  488. break;
  489. }
  490. num = atoi(num_buf);
  491. if (isdigit(num_buf[0]) && (num <= 100)) {
  492. #if DEBUG
  493. DEBUG_MESSAGE(itoa(num));
  494. #endif
  495. fb_drawprogressbar(num);
  496. }
  497. free(num_buf);
  498. }
  499. if (bCursorOff) // restore cursor
  500. full_write(STDOUT_FILENO, ESC"[?25h", 6);
  501. return EXIT_SUCCESS;
  502. }