early_printk.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (C) 2013 Intel Corporation; author Matt Fleming
  3. *
  4. * This file is part of the Linux kernel, and is made available under
  5. * the terms of the GNU General Public License version 2.
  6. */
  7. #include <linux/console.h>
  8. #include <linux/efi.h>
  9. #include <linux/font.h>
  10. #include <linux/io.h>
  11. #include <linux/kernel.h>
  12. #include <asm/setup.h>
  13. static const struct font_desc *font;
  14. static u32 efi_x, efi_y;
  15. static void *efi_fb;
  16. static bool early_efi_keep;
  17. /*
  18. * efi earlyprintk need use early_ioremap to map the framebuffer.
  19. * But early_ioremap is not usable for earlyprintk=efi,keep, ioremap should
  20. * be used instead. ioremap will be available after paging_init() which is
  21. * earlier than initcall callbacks. Thus adding this early initcall function
  22. * early_efi_map_fb to map the whole efi framebuffer.
  23. */
  24. static __init int early_efi_map_fb(void)
  25. {
  26. unsigned long base, size;
  27. if (!early_efi_keep)
  28. return 0;
  29. base = boot_params.screen_info.lfb_base;
  30. size = boot_params.screen_info.lfb_size;
  31. efi_fb = ioremap(base, size);
  32. return efi_fb ? 0 : -ENOMEM;
  33. }
  34. early_initcall(early_efi_map_fb);
  35. /*
  36. * early_efi_map maps efi framebuffer region [start, start + len -1]
  37. * In case earlyprintk=efi,keep we have the whole framebuffer mapped already
  38. * so just return the offset efi_fb + start.
  39. */
  40. static __ref void *early_efi_map(unsigned long start, unsigned long len)
  41. {
  42. unsigned long base;
  43. base = boot_params.screen_info.lfb_base;
  44. if (efi_fb)
  45. return (efi_fb + start);
  46. else
  47. return early_ioremap(base + start, len);
  48. }
  49. static __ref void early_efi_unmap(void *addr, unsigned long len)
  50. {
  51. if (!efi_fb)
  52. early_iounmap(addr, len);
  53. }
  54. static void early_efi_clear_scanline(unsigned int y)
  55. {
  56. unsigned long *dst;
  57. u16 len;
  58. len = boot_params.screen_info.lfb_linelength;
  59. dst = early_efi_map(y*len, len);
  60. if (!dst)
  61. return;
  62. memset(dst, 0, len);
  63. early_efi_unmap(dst, len);
  64. }
  65. static void early_efi_scroll_up(void)
  66. {
  67. unsigned long *dst, *src;
  68. u16 len;
  69. u32 i, height;
  70. len = boot_params.screen_info.lfb_linelength;
  71. height = boot_params.screen_info.lfb_height;
  72. for (i = 0; i < height - font->height; i++) {
  73. dst = early_efi_map(i*len, len);
  74. if (!dst)
  75. return;
  76. src = early_efi_map((i + font->height) * len, len);
  77. if (!src) {
  78. early_efi_unmap(dst, len);
  79. return;
  80. }
  81. memmove(dst, src, len);
  82. early_efi_unmap(src, len);
  83. early_efi_unmap(dst, len);
  84. }
  85. }
  86. static void early_efi_write_char(u32 *dst, unsigned char c, unsigned int h)
  87. {
  88. const u32 color_black = 0x00000000;
  89. const u32 color_white = 0x00ffffff;
  90. const u8 *src;
  91. u8 s8;
  92. int m;
  93. src = font->data + c * font->height;
  94. s8 = *(src + h);
  95. for (m = 0; m < 8; m++) {
  96. if ((s8 >> (7 - m)) & 1)
  97. *dst = color_white;
  98. else
  99. *dst = color_black;
  100. dst++;
  101. }
  102. }
  103. static void
  104. early_efi_write(struct console *con, const char *str, unsigned int num)
  105. {
  106. struct screen_info *si;
  107. unsigned int len;
  108. const char *s;
  109. void *dst;
  110. si = &boot_params.screen_info;
  111. len = si->lfb_linelength;
  112. while (num) {
  113. unsigned int linemax;
  114. unsigned int h, count = 0;
  115. for (s = str; *s && *s != '\n'; s++) {
  116. if (count == num)
  117. break;
  118. count++;
  119. }
  120. linemax = (si->lfb_width - efi_x) / font->width;
  121. if (count > linemax)
  122. count = linemax;
  123. for (h = 0; h < font->height; h++) {
  124. unsigned int n, x;
  125. dst = early_efi_map((efi_y + h) * len, len);
  126. if (!dst)
  127. return;
  128. s = str;
  129. n = count;
  130. x = efi_x;
  131. while (n-- > 0) {
  132. early_efi_write_char(dst + x*4, *s, h);
  133. x += font->width;
  134. s++;
  135. }
  136. early_efi_unmap(dst, len);
  137. }
  138. num -= count;
  139. efi_x += count * font->width;
  140. str += count;
  141. if (num > 0 && *s == '\n') {
  142. efi_x = 0;
  143. efi_y += font->height;
  144. str++;
  145. num--;
  146. }
  147. if (efi_x >= si->lfb_width) {
  148. efi_x = 0;
  149. efi_y += font->height;
  150. }
  151. if (efi_y + font->height > si->lfb_height) {
  152. u32 i;
  153. efi_y -= font->height;
  154. early_efi_scroll_up();
  155. for (i = 0; i < font->height; i++)
  156. early_efi_clear_scanline(efi_y + i);
  157. }
  158. }
  159. }
  160. static __init int early_efi_setup(struct console *con, char *options)
  161. {
  162. struct screen_info *si;
  163. u16 xres, yres;
  164. u32 i;
  165. si = &boot_params.screen_info;
  166. xres = si->lfb_width;
  167. yres = si->lfb_height;
  168. /*
  169. * early_efi_write_char() implicitly assumes a framebuffer with
  170. * 32-bits per pixel.
  171. */
  172. if (si->lfb_depth != 32)
  173. return -ENODEV;
  174. font = get_default_font(xres, yres, -1, -1);
  175. if (!font)
  176. return -ENODEV;
  177. efi_y = rounddown(yres, font->height) - font->height;
  178. for (i = 0; i < (yres - efi_y) / font->height; i++)
  179. early_efi_scroll_up();
  180. /* early_console_register will unset CON_BOOT in case ,keep */
  181. if (!(con->flags & CON_BOOT))
  182. early_efi_keep = true;
  183. return 0;
  184. }
  185. struct console early_efi_console = {
  186. .name = "earlyefi",
  187. .write = early_efi_write,
  188. .setup = early_efi_setup,
  189. .flags = CON_PRINTBUFFER,
  190. .index = -1,
  191. };