bootcon.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* Direct-to-LFB boot console (see bootcon.h).
  2. *
  3. * This deliberately does not use vga_fb_width()/vga_fb_*(): those call
  4. * vga_cache_boot_fb(), which probes the GPU and maps BARs, and this code runs
  5. * before mm_init/paging_init exist. It reads the boot framebuffer info block
  6. * at 0x600 directly, exactly like paging.c does.
  7. */
  8. #include "bootcon.h"
  9. #include "bootinfo.h"
  10. #include "font_8x8.h"
  11. #include "mm.h"
  12. #include "serial.h"
  13. #include "vga.h"
  14. #define SCALE 2
  15. #define LINE_H (8 * SCALE + 2)
  16. #define LEFT 8
  17. #define TOP 8
  18. static unsigned int fb_w, fb_h, fb_pitch, fb_bpp;
  19. static unsigned int fb_base;
  20. static volatile unsigned char *fb_lfb;
  21. static int fb_swap;
  22. static int fb_ok;
  23. static int enabled;
  24. static unsigned int cur_x, cur_y;
  25. static unsigned int home_x, home_y;
  26. /* Text log kept independently of the framebuffer: bootcon is disabled when
  27. * the WM starts, but the e1000e probe and later driver messages still go
  28. * through bootcon_puts(). The buffer is allocated from the frame allocator
  29. * (see bootcon_log_init) so it does not move the kernel's _end past the low
  30. * rootfs pack at 0x60000. */
  31. static char *log_buf;
  32. static unsigned int log_cap;
  33. static unsigned int log_len;
  34. static int log_truncated;
  35. static void fb_px(unsigned int x, unsigned int y, unsigned int r,
  36. unsigned int g, unsigned int b)
  37. {
  38. volatile unsigned char *p;
  39. if (!fb_ok || x >= fb_w || y >= fb_h)
  40. return;
  41. p = fb_lfb + (unsigned long)y * fb_pitch +
  42. (unsigned long)x * (fb_bpp / 8);
  43. if (fb_bpp == 16) {
  44. unsigned int v = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  45. p[0] = (unsigned char)(v & 0xFF);
  46. p[1] = (unsigned char)(v >> 8);
  47. } else {
  48. p[0] = (unsigned char)(fb_swap ? r : b);
  49. p[1] = (unsigned char)g;
  50. p[2] = (unsigned char)(fb_swap ? b : r);
  51. if (fb_bpp == 32)
  52. p[3] = 0;
  53. }
  54. }
  55. /* Clear the band the next line will be drawn into, so a reused line (after
  56. * wrapping at the bottom) cannot leave parts of an older message behind. */
  57. static void clear_line(void)
  58. {
  59. unsigned int x, y;
  60. for (y = 0; y < LINE_H; y++)
  61. for (x = 0; x < fb_w; x++)
  62. fb_px(x, cur_y + y, 0, 0, 0);
  63. }
  64. static void draw_char(unsigned int x, unsigned int y, unsigned char c)
  65. {
  66. const unsigned char *g = &font_8x8[(unsigned int)c * 8];
  67. unsigned int row, col;
  68. for (row = 0; row < 8; row++) {
  69. unsigned char bits = g[row];
  70. for (col = 0; col < 8; col++) {
  71. if (bits & (0x80u >> col)) {
  72. unsigned int i, j;
  73. for (i = 0; i < SCALE; i++)
  74. for (j = 0; j < SCALE; j++)
  75. fb_px(x + col * SCALE + i,
  76. y + row * SCALE + j,
  77. 255, 255, 255);
  78. }
  79. }
  80. }
  81. }
  82. void bootcon_init(void)
  83. {
  84. const volatile struct vga_boot_fb_info *fb = VGA_BOOT_FB;
  85. /* Opaque asm hides the fixed 0x600 pointer from -Warray-bounds. */
  86. __asm__ volatile ("" : "+r"(fb));
  87. fb_ok = 0;
  88. enabled = 0;
  89. cur_x = LEFT;
  90. cur_y = TOP;
  91. if (!fb->valid || !fb->lfb_base)
  92. return;
  93. fb_w = fb->width;
  94. fb_h = fb->height;
  95. fb_pitch = fb->pitch;
  96. fb_bpp = fb->bpp;
  97. fb_base = fb->lfb_base;
  98. if (fb_bpp != 16 && fb_bpp != 24 && fb_bpp != 32)
  99. return;
  100. /* Some BIOSes report a pitch in pixels or too small; enforce a sane
  101. * minimum exactly like vga_cache_boot_fb() does. */
  102. if (fb_pitch < fb_w * (fb_bpp / 8))
  103. fb_pitch = fb_w * (fb_bpp / 8);
  104. fb_lfb = (volatile unsigned char *)(unsigned long)fb->lfb_base;
  105. fb_swap = bootinfo_rgbx();
  106. if (!fb_w || !fb_h || !fb_pitch || !fb_lfb)
  107. return;
  108. fb_ok = 1;
  109. enabled = 1;
  110. /* Start away from the very top-left: some VBIOSes centre a mode that
  111. * does not match the panel (e.g. 1280x1024 on a 1280x800 LVDS), which
  112. * crops the outer rows/columns. Starting at 1/8,1/4 keeps the log
  113. * inside the visible area in both the centred and the scaled case. */
  114. home_x = fb_w / 8;
  115. home_y = fb_h / 4;
  116. if (home_x < 8)
  117. home_x = 8;
  118. if (home_y < 8)
  119. home_y = 8;
  120. cur_x = home_x;
  121. cur_y = home_y;
  122. clear_line();
  123. }
  124. int bootcon_ok(void)
  125. {
  126. return fb_ok;
  127. }
  128. void bootcon_set_enabled(int on)
  129. {
  130. enabled = on && fb_ok;
  131. if (enabled)
  132. clear_line();
  133. }
  134. static void newline(void)
  135. {
  136. cur_x = home_x;
  137. cur_y += LINE_H;
  138. if (cur_y + 8 * SCALE > fb_h)
  139. cur_y = home_y;
  140. clear_line();
  141. }
  142. void bootcon_log_init(void)
  143. {
  144. unsigned int mem;
  145. if (log_buf)
  146. return;
  147. mem = mm_alloc_pages(BOOTCON_LOG_PAGES);
  148. if (!mem)
  149. return;
  150. log_buf = (char *)mem;
  151. log_cap = BOOTCON_LOG_PAGES * PAGE_SIZE;
  152. log_len = 0;
  153. log_truncated = 0;
  154. log_buf[0] = '\0';
  155. }
  156. void bootcon_puts(const char *s)
  157. {
  158. while (*s) {
  159. unsigned char c = (unsigned char)*s++;
  160. /* Keep the text even after bootcon_set_enabled(0) has hidden
  161. * the framebuffer console. */
  162. if (log_buf) {
  163. if (log_len + 1 < log_cap) {
  164. log_buf[log_len++] = (char)c;
  165. log_buf[log_len] = '\0';
  166. } else {
  167. log_truncated = 1;
  168. }
  169. }
  170. if (!fb_ok || !enabled)
  171. continue;
  172. if (c == '\r') {
  173. cur_x = LEFT;
  174. continue;
  175. }
  176. if (c == '\n') {
  177. newline();
  178. continue;
  179. }
  180. if (cur_x + 8 * SCALE > fb_w)
  181. newline();
  182. draw_char(cur_x, cur_y, c);
  183. cur_x += 8 * SCALE;
  184. }
  185. }
  186. const char *bootcon_log(unsigned int *len)
  187. {
  188. *len = log_len;
  189. return log_buf ? log_buf : "";
  190. }
  191. int bootcon_log_truncated(void)
  192. {
  193. return log_truncated;
  194. }
  195. void bootcon_dec(unsigned int v)
  196. {
  197. char buf[12];
  198. unsigned int i = 0;
  199. if (v == 0) {
  200. bootcon_puts("0");
  201. return;
  202. }
  203. while (v) {
  204. buf[i++] = (char)('0' + v % 10);
  205. v /= 10;
  206. }
  207. while (i) {
  208. char one[2];
  209. one[0] = buf[--i];
  210. one[1] = '\0';
  211. bootcon_puts(one);
  212. }
  213. }
  214. void bootcon_hex(unsigned int v)
  215. {
  216. static const char hx[] = "0123456789ABCDEF";
  217. char buf[9];
  218. unsigned int i;
  219. for (i = 0; i < 8; i++)
  220. buf[i] = hx[(v >> (4 * (7 - i))) & 0xF];
  221. buf[8] = '\0';
  222. bootcon_puts(buf);
  223. }
  224. void bootcon_fb_info(void)
  225. {
  226. bootcon_puts("fb ");
  227. bootcon_dec(fb_w);
  228. bootcon_puts("x");
  229. bootcon_dec(fb_h);
  230. bootcon_puts("x");
  231. bootcon_dec(fb_bpp);
  232. bootcon_puts(" pitch ");
  233. bootcon_dec(fb_pitch);
  234. bootcon_puts(" lfb 0x");
  235. bootcon_hex(fb_base);
  236. bootcon_puts("\n");
  237. /* Mirror to COM1 (and the netlog console) so a machine without a
  238. * serial port can report the VBE mode before the WM repaints. */
  239. serial_puts(SERIAL_COM1, "fb ");
  240. serial_putdec(SERIAL_COM1, fb_w);
  241. serial_puts(SERIAL_COM1, "x");
  242. serial_putdec(SERIAL_COM1, fb_h);
  243. serial_puts(SERIAL_COM1, "x");
  244. serial_putdec(SERIAL_COM1, fb_bpp);
  245. serial_puts(SERIAL_COM1, " pitch ");
  246. serial_putdec(SERIAL_COM1, fb_pitch);
  247. serial_puts(SERIAL_COM1, " lfb ");
  248. serial_puthex(SERIAL_COM1, fb_base);
  249. serial_puts(SERIAL_COM1, "\n");
  250. }