ucmd_bootscan.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* bootscan: live tail of the boot storage-scan kernel thread's output.
  2. *
  3. * The kernel captures every COM1 character boot_storage_main() writes (the
  4. * vfs/blk mount trail, the driver probes and the final boot traces) into a
  5. * ring exposed by SYS_BOOTLOG. The WM spawns this window before the first
  6. * terminal, so the terminal is created last and keeps the keyboard focus.
  7. *
  8. * The view follows the tail while the scan runs and keeps the scrollback
  9. * afterwards. Wheel, PgUp/PgDn, Up/Down, Home and End scroll; scrolling up
  10. * stops the follow until the bottom is reached again. Esc closes it.
  11. */
  12. #include "ucmd_lib.h"
  13. #include "ngui.h"
  14. #define TEXT_MAX (32 * 1024)
  15. #define POLL_MS 100
  16. #define LINE_PX 12
  17. #define PAD 8
  18. #define FOOT_H 20
  19. static struct ngui ui;
  20. static struct ngui_scroll sc;
  21. static char text[TEXT_MAX];
  22. static unsigned int text_len;
  23. static unsigned int seen;
  24. static int follow = 1;
  25. static int pending_tail;
  26. static int have_info;
  27. static int scan_active;
  28. static unsigned int scan_cap;
  29. static int quit;
  30. static void append_text(const char *p, unsigned int n)
  31. {
  32. unsigned int i;
  33. for (i = 0; i < n && text_len + 1 < TEXT_MAX; i++)
  34. text[text_len++] = p[i];
  35. }
  36. /* Fetch every byte that appeared since the last poll. Returns 1 when the
  37. * view changed. */
  38. static int pull(void)
  39. {
  40. struct sys_bootlog info;
  41. int changed = 0;
  42. int passes = 0;
  43. if (u_bootlog_stat(&info) != 0)
  44. return 0;
  45. if (!have_info || info.active != (unsigned int)scan_active ||
  46. info.capacity != scan_cap) {
  47. have_info = 1;
  48. scan_active = (int)info.active;
  49. scan_cap = info.capacity;
  50. changed = 1;
  51. }
  52. if (!info.capacity)
  53. return changed;
  54. if (seen < info.start || seen > info.total)
  55. seen = info.start;
  56. while (seen < info.total && passes++ < 64) {
  57. char chunk[SYS_BOOTLOG_CHUNK];
  58. int n = u_bootlog_read(seen, chunk);
  59. if (n < 0) {
  60. /* The ring wrapped between the two calls. */
  61. if (u_bootlog_stat(&info) != 0)
  62. break;
  63. if (seen < info.start || seen > info.total)
  64. seen = info.start;
  65. if (seen >= info.total)
  66. break;
  67. continue;
  68. }
  69. if (n == 0)
  70. break;
  71. append_text(chunk, (unsigned int)n);
  72. seen += (unsigned int)n;
  73. changed = 1;
  74. if ((unsigned int)n < SYS_BOOTLOG_CHUNK)
  75. break;
  76. }
  77. return changed;
  78. }
  79. static unsigned int count_lines(void)
  80. {
  81. unsigned int i, n = 0;
  82. if (!text_len)
  83. return 0;
  84. for (i = 0; i < text_len; i++)
  85. if (text[i] == '\n')
  86. n++;
  87. if (text[text_len - 1] != '\n')
  88. n++;
  89. return n;
  90. }
  91. static unsigned short line_color(const char *s, unsigned int n)
  92. {
  93. if (n >= 4 && s[0] == 'v' && s[1] == 'f' && s[2] == 's' &&
  94. s[3] == ':')
  95. return ui.theme.accent;
  96. if (n >= 4 && s[0] == 'b' && s[1] == 'l' && s[2] == 'k' &&
  97. s[3] == ':')
  98. return ui.theme.text_dim;
  99. if (n >= 6 && s[0] == '[' && s[1] == 'b' && s[2] == 'o' &&
  100. s[3] == 'o' && s[4] == 't' && s[5] == ']')
  101. return ui.theme.text_dim;
  102. return ui.theme.text;
  103. }
  104. static void draw_footer(struct ngui_rect r)
  105. {
  106. const char *status;
  107. const char *hint = "PgUp/PgDn scroll | Esc close";
  108. unsigned short color;
  109. int hint_w;
  110. if (!have_info || (!scan_cap && !scan_active))
  111. status = "waiting for the boot scan...";
  112. else if (!scan_cap)
  113. status = "boot scan log unavailable";
  114. else if (scan_active)
  115. status = "live: boot scan running";
  116. else
  117. status = "boot scan finished";
  118. color = scan_active ? (scan_cap ? ui.theme.success : ui.theme.warning) :
  119. ui.theme.text_dim;
  120. ngui_fill_rect(&ui, r, ui.theme.panel);
  121. ngui_text(&ui, r.x + 2, r.y + 3, status, color);
  122. hint_w = (int)ngui_text_width_ui(&ui, hint);
  123. if (hint_w + 120 < r.w)
  124. ngui_text(&ui, r.x + r.w - hint_w - 2, r.y + 3, hint,
  125. ui.theme.text_dim);
  126. }
  127. static void draw(void)
  128. {
  129. struct ngui_rect body, foot;
  130. int line_h = ngui_text_line_height(&ui, LINE_PX, 1);
  131. unsigned int lines = count_lines();
  132. unsigned int pos = 0, idx = 0;
  133. int max, y;
  134. if (line_h < 10)
  135. line_h = 10;
  136. body.x = PAD;
  137. body.y = PAD;
  138. body.w = ui.w - 2 * PAD;
  139. body.h = ui.h - 2 * PAD - FOOT_H;
  140. if (body.w < 20)
  141. body.w = 20;
  142. if (body.h < 20)
  143. body.h = 20;
  144. foot.x = 0;
  145. foot.y = ui.h - FOOT_H;
  146. foot.w = ui.w;
  147. foot.h = FOOT_H;
  148. ngui_fill_rect(&ui, (struct ngui_rect){ 0, 0, ui.w, ui.h },
  149. ui.theme.bg);
  150. sc.content_h = (int)lines * line_h;
  151. ngui_scroll_begin(&ui, body, &sc);
  152. ngui_scroll_wheel(&ui, body, &sc);
  153. max = sc.content_h - sc.view_h;
  154. if (max < 0)
  155. max = 0;
  156. if (pending_tail && follow)
  157. ngui_scroll_to(&sc, max);
  158. pending_tail = 0;
  159. follow = sc.offset >= max - 1;
  160. while (pos < text_len) {
  161. unsigned int start = pos, end;
  162. while (pos < text_len && text[pos] != '\n')
  163. pos++;
  164. end = pos;
  165. y = body.y + (int)idx * line_h - sc.offset;
  166. if (y + line_h > body.y && y < body.y + body.h) {
  167. ngui_text_px(&ui, body.x + 2, y + 1, text + start,
  168. end - start, line_color(text + start,
  169. end - start),
  170. LINE_PX, 0, 0, 1);
  171. }
  172. idx++;
  173. if (pos < text_len && text[pos] == '\n')
  174. pos++;
  175. }
  176. ngui_scroll_end(&ui, body, &sc);
  177. draw_footer(foot);
  178. }
  179. static void scroll_keys(void)
  180. {
  181. int k;
  182. int max = sc.content_h - sc.view_h;
  183. int page = sc.view_h - 22;
  184. if (page < 22)
  185. page = 22;
  186. if (max < 0)
  187. max = 0;
  188. while ((k = ngui_key(&ui)) != 0) {
  189. int old = sc.offset;
  190. if (k == NGUI_KEY_ESC)
  191. quit = 1;
  192. else if (k == NGUI_KEY_PAGE_UP)
  193. ngui_scroll_to(&sc, sc.offset - page);
  194. else if (k == NGUI_KEY_PAGE_DOWN)
  195. ngui_scroll_to(&sc, sc.offset + page);
  196. else if (k == NGUI_KEY_UP)
  197. ngui_scroll_to(&sc, sc.offset - 20);
  198. else if (k == NGUI_KEY_DOWN)
  199. ngui_scroll_to(&sc, sc.offset + 20);
  200. else if (k == NGUI_KEY_HOME)
  201. ngui_scroll_to(&sc, 0);
  202. else if (k == NGUI_KEY_END)
  203. ngui_scroll_to(&sc, max);
  204. if (sc.offset != old)
  205. ngui_invalidate(&ui);
  206. }
  207. }
  208. void _start(const char *arg)
  209. {
  210. struct win_info wi;
  211. unsigned int sw = 0, sh = 0;
  212. unsigned int w = 440, h = 300;
  213. unsigned int last_poll;
  214. (void)arg;
  215. if (u_screen_size(&sw, &sh) == 0) {
  216. if (sw > 80 && w > sw - 40)
  217. w = sw - 40;
  218. if (sh > 200 && h > sh - 120)
  219. h = sh - 120;
  220. }
  221. if (w < 260)
  222. w = 260;
  223. if (h < 180)
  224. h = 180;
  225. if (u_win_create(w, h, "Boot scan", &wi) != 0) {
  226. u_puts("bootscan: no graphics window\n");
  227. u_exit(1);
  228. }
  229. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  230. ngui_font_attach(&ui);
  231. sc.hide_bar = 0;
  232. last_poll = u_ticks_ms() - POLL_MS;
  233. for (;;) {
  234. unsigned int now = u_ticks_ms();
  235. if ((int)(now - (last_poll + POLL_MS)) >= 0) {
  236. last_poll = now;
  237. if (pull()) {
  238. pending_tail = 1;
  239. ngui_invalidate(&ui);
  240. }
  241. }
  242. if (!ngui_begin(&ui)) {
  243. ngui_end_ex(&ui, 0);
  244. u_sleep_ms(POLL_MS);
  245. continue;
  246. }
  247. scroll_keys();
  248. if (quit)
  249. break;
  250. draw();
  251. ngui_end(&ui);
  252. u_sleep_ms(POLL_MS);
  253. }
  254. u_win_close();
  255. u_exit(0);
  256. }