virtio_console.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env node
  2. "use strict";
  3. process.on("unhandledRejection", exn => { throw exn; });
  4. const TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;
  5. const SHOW_LOGS = false;
  6. var V86 = require(`../../build/${TEST_RELEASE_BUILD ? "libv86" : "libv86-debug"}.js`).V86;
  7. const fs = require("fs");
  8. const emulator = new V86({
  9. bios: { url: __dirname + "/../../bios/seabios.bin" },
  10. vga_bios: { url: __dirname + "/../../bios/vgabios.bin" },
  11. cdrom: { url: __dirname + "/../../images/linux4.iso" },
  12. autostart: true,
  13. memory_size: 512 * 1024 * 1024,
  14. bzimage_initrd_from_filesystem: true,
  15. cmdline: [
  16. "console=ttyS0",
  17. "rw apm=off vga=0x344 video=vesafb:ypan,vremap:8",
  18. "root=host9p rootfstype=9p rootflags=trans=virtio,cache=loose mitigations=off",
  19. "audit=0 init=/usr/bin/init-openrc net.ifnames=0 biosdevname=0",
  20. ].join(" "),
  21. filesystem: {
  22. basefs: "images/fs.json",
  23. baseurl: "images/arch/",
  24. },
  25. disable_jit: +process.env.DISABLE_JIT,
  26. log_level: SHOW_LOGS ? 0x400000 : 0,
  27. virtio_console: true,
  28. });
  29. let line = "";
  30. let sent_command = false;
  31. emulator.add_listener("serial0-output-byte", function(byte)
  32. {
  33. var chr = String.fromCharCode(byte);
  34. process.stdout.write(chr);
  35. if(chr === "\n")
  36. {
  37. line = "";
  38. }
  39. else
  40. {
  41. line += chr;
  42. }
  43. // TODO: use better prompt detection once it's configured to not print colours
  44. if(!sent_command && line.endsWith("# ") && line.includes("root@localhost"))
  45. {
  46. sent_command = true;
  47. emulator.serial0_send("lspci -vv; /etc/openrc/init.d/udev start; echo ping > /dev/hvc0\n");
  48. }
  49. if(line.endsWith("pong"))
  50. {
  51. console.log("\nTest passed");
  52. emulator.stop();
  53. }
  54. });
  55. let got_output = false;
  56. emulator.add_listener("virtio-console0-output-bytes", function(bytes)
  57. {
  58. if(!got_output)
  59. {
  60. got_output = true;
  61. console.log("From virtio console:", String.fromCharCode.apply(String, bytes));
  62. emulator.serial0_send("cat /dev/hvc0\n");
  63. setTimeout(() => {
  64. emulator.bus.send("virtio-console0-input-bytes", Uint8Array.from(Buffer.from("pong\n")));
  65. }, 5000);
  66. }
  67. });