bootcon.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 "serial.h"
  12. #include "vga.h"
  13. #define SCALE 2
  14. #define LINE_H (8 * SCALE + 2)
  15. #define LEFT 8
  16. #define TOP 8
  17. static unsigned int fb_w, fb_h, fb_pitch, fb_bpp;
  18. static unsigned int fb_base;
  19. static volatile unsigned char *fb_lfb;
  20. static int fb_swap;
  21. static int fb_ok;
  22. static int enabled;
  23. static unsigned int cur_x, cur_y;
  24. static unsigned int home_x, home_y;
  25. static void fb_px(unsigned int x, unsigned int y, unsigned int r,
  26. unsigned int g, unsigned int b)
  27. {
  28. volatile unsigned char *p;
  29. if (!fb_ok || x >= fb_w || y >= fb_h)
  30. return;
  31. p = fb_lfb + (unsigned long)y * fb_pitch +
  32. (unsigned long)x * (fb_bpp / 8);
  33. if (fb_bpp == 16) {
  34. unsigned int v = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  35. p[0] = (unsigned char)(v & 0xFF);
  36. p[1] = (unsigned char)(v >> 8);
  37. } else {
  38. p[0] = (unsigned char)(fb_swap ? r : b);
  39. p[1] = (unsigned char)g;
  40. p[2] = (unsigned char)(fb_swap ? b : r);
  41. if (fb_bpp == 32)
  42. p[3] = 0;
  43. }
  44. }
  45. /* Clear the band the next line will be drawn into, so a reused line (after
  46. * wrapping at the bottom) cannot leave parts of an older message behind. */
  47. static void clear_line(void)
  48. {
  49. unsigned int x, y;
  50. for (y = 0; y < LINE_H; y++)
  51. for (x = 0; x < fb_w; x++)
  52. fb_px(x, cur_y + y, 0, 0, 0);
  53. }
  54. static void draw_char(unsigned int x, unsigned int y, unsigned char c)
  55. {
  56. const unsigned char *g = &font_8x8[(unsigned int)c * 8];
  57. unsigned int row, col;
  58. for (row = 0; row < 8; row++) {
  59. unsigned char bits = g[row];
  60. for (col = 0; col < 8; col++) {
  61. if (bits & (0x80u >> col)) {
  62. unsigned int i, j;
  63. for (i = 0; i < SCALE; i++)
  64. for (j = 0; j < SCALE; j++)
  65. fb_px(x + col * SCALE + i,
  66. y + row * SCALE + j,
  67. 255, 255, 255);
  68. }
  69. }
  70. }
  71. }
  72. void bootcon_init(void)
  73. {
  74. const volatile struct vga_boot_fb_info *fb = VGA_BOOT_FB;
  75. /* Opaque asm hides the fixed 0x600 pointer from -Warray-bounds. */
  76. __asm__ volatile ("" : "+r"(fb));
  77. fb_ok = 0;
  78. enabled = 0;
  79. cur_x = LEFT;
  80. cur_y = TOP;
  81. if (!fb->valid || !fb->lfb_base)
  82. return;
  83. fb_w = fb->width;
  84. fb_h = fb->height;
  85. fb_pitch = fb->pitch;
  86. fb_bpp = fb->bpp;
  87. fb_base = fb->lfb_base;
  88. if (fb_bpp != 16 && fb_bpp != 24 && fb_bpp != 32)
  89. return;
  90. /* Some BIOSes report a pitch in pixels or too small; enforce a sane
  91. * minimum exactly like vga_cache_boot_fb() does. */
  92. if (fb_pitch < fb_w * (fb_bpp / 8))
  93. fb_pitch = fb_w * (fb_bpp / 8);
  94. fb_lfb = (volatile unsigned char *)(unsigned long)fb->lfb_base;
  95. fb_swap = bootinfo_rgbx();
  96. if (!fb_w || !fb_h || !fb_pitch || !fb_lfb)
  97. return;
  98. fb_ok = 1;
  99. enabled = 1;
  100. /* Start away from the very top-left: some VBIOSes centre a mode that
  101. * does not match the panel (e.g. 1280x1024 on a 1280x800 LVDS), which
  102. * crops the outer rows/columns. Starting at 1/8,1/4 keeps the log
  103. * inside the visible area in both the centred and the scaled case. */
  104. home_x = fb_w / 8;
  105. home_y = fb_h / 4;
  106. if (home_x < 8)
  107. home_x = 8;
  108. if (home_y < 8)
  109. home_y = 8;
  110. cur_x = home_x;
  111. cur_y = home_y;
  112. clear_line();
  113. }
  114. int bootcon_ok(void)
  115. {
  116. return fb_ok;
  117. }
  118. void bootcon_set_enabled(int on)
  119. {
  120. enabled = on && fb_ok;
  121. if (enabled)
  122. clear_line();
  123. }
  124. static void newline(void)
  125. {
  126. cur_x = home_x;
  127. cur_y += LINE_H;
  128. if (cur_y + 8 * SCALE > fb_h)
  129. cur_y = home_y;
  130. clear_line();
  131. }
  132. void bootcon_puts(const char *s)
  133. {
  134. if (!fb_ok || !enabled)
  135. return;
  136. while (*s) {
  137. unsigned char c = (unsigned char)*s++;
  138. if (c == '\r') {
  139. cur_x = LEFT;
  140. continue;
  141. }
  142. if (c == '\n') {
  143. newline();
  144. continue;
  145. }
  146. if (cur_x + 8 * SCALE > fb_w)
  147. newline();
  148. draw_char(cur_x, cur_y, c);
  149. cur_x += 8 * SCALE;
  150. }
  151. }
  152. void bootcon_dec(unsigned int v)
  153. {
  154. char buf[12];
  155. unsigned int i = 0;
  156. if (v == 0) {
  157. bootcon_puts("0");
  158. return;
  159. }
  160. while (v) {
  161. buf[i++] = (char)('0' + v % 10);
  162. v /= 10;
  163. }
  164. while (i) {
  165. char one[2];
  166. one[0] = buf[--i];
  167. one[1] = '\0';
  168. bootcon_puts(one);
  169. }
  170. }
  171. void bootcon_hex(unsigned int v)
  172. {
  173. static const char hx[] = "0123456789ABCDEF";
  174. char buf[9];
  175. unsigned int i;
  176. for (i = 0; i < 8; i++)
  177. buf[i] = hx[(v >> (4 * (7 - i))) & 0xF];
  178. buf[8] = '\0';
  179. bootcon_puts(buf);
  180. }
  181. void bootcon_fb_info(void)
  182. {
  183. bootcon_puts("fb ");
  184. bootcon_dec(fb_w);
  185. bootcon_puts("x");
  186. bootcon_dec(fb_h);
  187. bootcon_puts("x");
  188. bootcon_dec(fb_bpp);
  189. bootcon_puts(" pitch ");
  190. bootcon_dec(fb_pitch);
  191. bootcon_puts(" lfb 0x");
  192. bootcon_hex(fb_base);
  193. bootcon_puts("\n");
  194. /* Mirror to COM1 (and the netlog console) so a machine without a
  195. * serial port can report the VBE mode before the WM repaints. */
  196. serial_puts(SERIAL_COM1, "fb ");
  197. serial_putdec(SERIAL_COM1, fb_w);
  198. serial_puts(SERIAL_COM1, "x");
  199. serial_putdec(SERIAL_COM1, fb_h);
  200. serial_puts(SERIAL_COM1, "x");
  201. serial_putdec(SERIAL_COM1, fb_bpp);
  202. serial_puts(SERIAL_COM1, " pitch ");
  203. serial_putdec(SERIAL_COM1, fb_pitch);
  204. serial_puts(SERIAL_COM1, " lfb ");
  205. serial_puthex(SERIAL_COM1, fb_base);
  206. serial_puts(SERIAL_COM1, "\n");
  207. }