early_printk.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 __init void early_efi_clear_scanline(unsigned int y)
  16. {
  17. unsigned long base, *dst;
  18. u16 len;
  19. base = boot_params.screen_info.lfb_base;
  20. len = boot_params.screen_info.lfb_linelength;
  21. dst = early_ioremap(base + y*len, len);
  22. if (!dst)
  23. return;
  24. memset(dst, 0, len);
  25. early_iounmap(dst, len);
  26. }
  27. static __init void early_efi_scroll_up(void)
  28. {
  29. unsigned long base, *dst, *src;
  30. u16 len;
  31. u32 i, height;
  32. base = boot_params.screen_info.lfb_base;
  33. len = boot_params.screen_info.lfb_linelength;
  34. height = boot_params.screen_info.lfb_height;
  35. for (i = 0; i < height - font->height; i++) {
  36. dst = early_ioremap(base + i*len, len);
  37. if (!dst)
  38. return;
  39. src = early_ioremap(base + (i + font->height) * len, len);
  40. if (!src) {
  41. early_iounmap(dst, len);
  42. return;
  43. }
  44. memmove(dst, src, len);
  45. early_iounmap(src, len);
  46. early_iounmap(dst, len);
  47. }
  48. }
  49. static void early_efi_write_char(u32 *dst, unsigned char c, unsigned int h)
  50. {
  51. const u32 color_black = 0x00000000;
  52. const u32 color_white = 0x00ffffff;
  53. const u8 *src;
  54. u8 s8;
  55. int m;
  56. src = font->data + c * font->height;
  57. s8 = *(src + h);
  58. for (m = 0; m < 8; m++) {
  59. if ((s8 >> (7 - m)) & 1)
  60. *dst = color_white;
  61. else
  62. *dst = color_black;
  63. dst++;
  64. }
  65. }
  66. static __init void
  67. early_efi_write(struct console *con, const char *str, unsigned int num)
  68. {
  69. struct screen_info *si;
  70. unsigned long base;
  71. unsigned int len;
  72. const char *s;
  73. void *dst;
  74. base = boot_params.screen_info.lfb_base;
  75. si = &boot_params.screen_info;
  76. len = si->lfb_linelength;
  77. while (num) {
  78. unsigned int linemax;
  79. unsigned int h, count = 0;
  80. for (s = str; *s && *s != '\n'; s++) {
  81. if (count == num)
  82. break;
  83. count++;
  84. }
  85. linemax = (si->lfb_width - efi_x) / font->width;
  86. if (count > linemax)
  87. count = linemax;
  88. for (h = 0; h < font->height; h++) {
  89. unsigned int n, x;
  90. dst = early_ioremap(base + (efi_y + h) * len, len);
  91. if (!dst)
  92. return;
  93. s = str;
  94. n = count;
  95. x = efi_x;
  96. while (n-- > 0) {
  97. early_efi_write_char(dst + x*4, *s, h);
  98. x += font->width;
  99. s++;
  100. }
  101. early_iounmap(dst, len);
  102. }
  103. num -= count;
  104. efi_x += count * font->width;
  105. str += count;
  106. if (num > 0 && *s == '\n') {
  107. efi_x = 0;
  108. efi_y += font->height;
  109. str++;
  110. num--;
  111. }
  112. if (efi_x >= si->lfb_width) {
  113. efi_x = 0;
  114. efi_y += font->height;
  115. }
  116. if (efi_y + font->height >= si->lfb_height) {
  117. u32 i;
  118. efi_y -= font->height;
  119. early_efi_scroll_up();
  120. for (i = 0; i < font->height; i++)
  121. early_efi_clear_scanline(efi_y + i);
  122. }
  123. }
  124. }
  125. static __init int early_efi_setup(struct console *con, char *options)
  126. {
  127. struct screen_info *si;
  128. u16 xres, yres;
  129. u32 i;
  130. si = &boot_params.screen_info;
  131. xres = si->lfb_width;
  132. yres = si->lfb_height;
  133. /*
  134. * early_efi_write_char() implicitly assumes a framebuffer with
  135. * 32-bits per pixel.
  136. */
  137. if (si->lfb_depth != 32)
  138. return -ENODEV;
  139. font = get_default_font(xres, yres, -1, -1);
  140. if (!font)
  141. return -ENODEV;
  142. efi_y = rounddown(yres, font->height) - font->height;
  143. for (i = 0; i < (yres - efi_y) / font->height; i++)
  144. early_efi_scroll_up();
  145. return 0;
  146. }
  147. struct console early_efi_console = {
  148. .name = "earlyefi",
  149. .write = early_efi_write,
  150. .setup = early_efi_setup,
  151. .flags = CON_PRINTBUFFER,
  152. .index = -1,
  153. };