splash-log.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * splash-log - Display startup messages before UI has started.
  7. */
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <stdint.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <fcntl.h>
  14. #include <sys/ioctl.h>
  15. #include <kernel/video.h>
  16. #include <toaru/pex.h>
  17. #include "terminal-font.h"
  18. /**
  19. * For legacy backwards-compatibility reasons, the VGA
  20. * text-mode window is normalled mapped 1:1. If this
  21. * ever changes, a few applications will need to be updated.
  22. */
  23. static unsigned short * textmemptr = (unsigned short *)0xB8000;
  24. static void placech(unsigned char c, int x, int y, int attr) {
  25. unsigned short *where;
  26. unsigned att = attr << 8;
  27. where = textmemptr + (y * 80 + x);
  28. *where = c | att;
  29. }
  30. /**
  31. * Graphical framebuffer is a bit more straightforward.
  32. */
  33. static int framebuffer_fd = -1;
  34. static int width, height, depth;
  35. static char * framebuffer;
  36. static void set_point(int x, int y, uint32_t value) {
  37. uint32_t * disp = (uint32_t *)framebuffer;
  38. uint32_t * cell = &disp[y * width + x];
  39. *cell = value;
  40. }
  41. #define BG_COLOR 0xFF050505
  42. #define FG_COLOR 0xFFCCCCCC
  43. #define char_width 9
  44. #define char_height 20
  45. static void write_char(int x, int y, int val, uint32_t color) {
  46. if (val > 128) {
  47. val = 4;
  48. }
  49. uint16_t * c = large_font[val];
  50. for (uint8_t i = 0; i < char_height; ++i) {
  51. for (uint8_t j = 0; j < char_width; ++j) {
  52. if (c[i] & (1 << (15-j))) {
  53. set_point(x+j,y+i,color);
  54. } else {
  55. set_point(x+j,y+i,BG_COLOR);
  56. }
  57. }
  58. }
  59. }
  60. static void update_message(char * c, int line) {
  61. if (framebuffer_fd != -1) {
  62. int x = 20;
  63. int y = 20 + char_height * line;
  64. while (*c) {
  65. write_char(x, y, *c, FG_COLOR);
  66. c++;
  67. x += char_width;
  68. }
  69. while (x < width - char_width) {
  70. write_char(x, y, ' ', FG_COLOR);
  71. x += char_width;
  72. }
  73. } else {
  74. int x = 2;
  75. int y = 2 + line;
  76. while (*c) {
  77. placech(*c, x, y, 0x7);
  78. c++;
  79. x++;
  80. }
  81. while (x < 80) {
  82. placech(' ', x, y, 0x7);
  83. x++;
  84. }
  85. }
  86. }
  87. static void clear_screen(void) {
  88. if (framebuffer_fd != -1) {
  89. for (int y = 0; y < height; ++y) {
  90. for (int x = 0; x < width; ++x) {
  91. set_point(x,y,BG_COLOR);
  92. }
  93. }
  94. } else {
  95. for (int y = 0; y < 24; ++y) {
  96. for (int x = 0; x < 80; ++x) {
  97. placech(' ', x, y, 0); /* Clear */
  98. }
  99. }
  100. }
  101. }
  102. static void check_framebuffer(void) {
  103. int tmpfd = open("/proc/framebuffer", O_RDONLY);
  104. if (tmpfd > 0) {
  105. framebuffer_fd = open("/dev/fb0", O_RDONLY);
  106. ioctl(framebuffer_fd, IO_VID_WIDTH, &width);
  107. ioctl(framebuffer_fd, IO_VID_HEIGHT, &height);
  108. ioctl(framebuffer_fd, IO_VID_DEPTH, &depth);
  109. ioctl(framebuffer_fd, IO_VID_ADDR, &framebuffer);
  110. ioctl(framebuffer_fd, IO_VID_SIGNAL, NULL);
  111. } else {
  112. framebuffer_fd = -1;
  113. }
  114. }
  115. static FILE * pex_endpoint = NULL;
  116. static void open_socket(void) {
  117. pex_endpoint = pex_bind("splash");
  118. if (!pex_endpoint) exit(1);
  119. }
  120. int main(int argc, char * argv[]) {
  121. if (getuid() != 0) {
  122. fprintf(stderr, "%s: only root should run this\n", argv[0]);
  123. return 1;
  124. }
  125. open_socket();
  126. if (!fork()) {
  127. check_framebuffer();
  128. clear_screen();
  129. update_message("ToaruOS is starting up...", 0);
  130. while (1) {
  131. pex_packet_t * p = calloc(PACKET_SIZE, 1);
  132. pex_listen(pex_endpoint, p);
  133. if (p->size < 4) continue; /* Ignore blank messages, erroneous line feeds, etc. */
  134. if (p->size > 80) continue; /* Ignore overly large messages */
  135. if (!strncmp((char*)p->data, "!quit", 5)) {
  136. /* Use the special message !quit to exit. */
  137. fclose(pex_endpoint);
  138. return 0;
  139. } else if (p->data[0] == ':') {
  140. /* Make sure message is nil terminated (it should be...) */
  141. char * tmp = malloc(p->size + 1);
  142. memcpy(tmp, p->data, p->size);
  143. tmp[p->size] = '\0';
  144. update_message(tmp+1, 1);
  145. free(tmp);
  146. } else {
  147. /* Make sure message is nil terminated (it should be...) */
  148. char * tmp = malloc(p->size + 1);
  149. memcpy(tmp, p->data, p->size);
  150. tmp[p->size] = '\0';
  151. update_message(tmp, 0);
  152. update_message("", 1);
  153. free(tmp);
  154. }
  155. }
  156. }
  157. return 0;
  158. }