main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of XNIX. */
  3. /*
  4. XNIX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. XNIX is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. /*
  16. You should have received a copy of the GNU General Public License
  17. along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <kernel/init.h>
  20. #include <kernel/errno.h>
  21. #include <kernel/types.h>
  22. #include <kernel/console.h>
  23. #include <kernel/vsprintf.h>
  24. #include <kernel/schk.h>
  25. #include <kernel/mm.h>
  26. #include <kernel/pci.h>
  27. #include <kernel/int.h>
  28. #include <kernel/sched.h>
  29. #include <kernel/keyboard.h>
  30. #include <kernel/console.h>
  31. #include <kernel/tty.h>
  32. #include <lib/string.h>
  33. #include <sys/syscall.h>
  34. #include <modules/beep.h>
  35. #include <modules/serial.h>
  36. #include <arch/io.h>
  37. #include <arch/sys.h>
  38. #include <arch/x86/int.h>
  39. #include <arch/x86/paging.h>
  40. /* we save boot-time arguments at that address where boot was. */
  41. struct bootarg *bootarg = (struct bootarg *) 0x7c00;
  42. void time_init(void);
  43. /* well, it looks like linux */
  44. #define CMOS_READ(addr) ({ \
  45. outb(addr, 0x70); \
  46. inb(0x71); \
  47. }) \
  48. /* only for byte */
  49. #define BCD2BIN(byte) ((byte) = ((byte) & 0xf) + ((byte) >> 4) * 10)
  50. void time_init(void);
  51. time_t mktime(struct time *);
  52. /*
  53. * init(void): initial all modules and devices.
  54. *
  55. * return value: error number. main will return after initial done.
  56. * if return value isn't 0, head.S will panic. else, it will halt.
  57. * */
  58. int init(void) {
  59. schk_init();
  60. console_init();
  61. print(red, "XNIX kernel starting...\n");
  62. int_init();
  63. mm_init();
  64. task_init();
  65. kbd_init();
  66. sti();
  67. printk(white, "total %d devices. \n", pci_lspci());
  68. printk(white, "System boots from the device %b: ", bootarg->bootdev);
  69. if (bootarg->bootdev & 0x80)
  70. printk(white, "hard disk %b. \n", bootarg->bootdev & 7);
  71. else
  72. printk(white, "floppy / flash memory / cd-rom %b. \n", bootarg->bootdev);
  73. time_init();
  74. serial_init();
  75. tty_write(&tty1, "hello, world. \n", 15);
  76. schk_leave();
  77. return 0;
  78. }
  79. void time_init(void) {
  80. schk_init();
  81. /* get current time, and save it to `startup_time` */
  82. struct time tm;
  83. /* it will execute when 'sec' changed for the second time */
  84. do {
  85. tm.sec = CMOS_READ(0);
  86. tm.min = CMOS_READ(2);
  87. tm.hour = CMOS_READ(4);
  88. tm.day = CMOS_READ(7);
  89. /* mouth is 1 - 12 */
  90. tm.month = CMOS_READ(8) - 1;
  91. /* this is the year only 2 bits (1970 -> 70) */
  92. tm.year = CMOS_READ(9);
  93. } while (tm.sec != CMOS_READ(0));
  94. BCD2BIN(tm.sec);
  95. BCD2BIN(tm.min);
  96. BCD2BIN(tm.hour);
  97. BCD2BIN(tm.day);
  98. BCD2BIN(tm.year);
  99. tm.year += tm.year >= 70 ? 1900 : 2000;
  100. printk(white, "current time: %d %d %d %d:%d:%d. \n",
  101. tm.year, tm.month + 1, tm.day, tm.hour, tm.min, tm.sec);
  102. printk(white, "current unix time-stamp: %d. \n", mktime(&tm));
  103. schk_leave();
  104. return;
  105. }
  106. static int month[] = {
  107. 0,
  108. 31,
  109. 31 + 29,
  110. 31 + 29 + 31,
  111. 31 + 29 + 31 + 30,
  112. 31 + 29 + 31 + 30 + 31,
  113. 31 + 29 + 31 + 30 + 31 + 30,
  114. 31 + 29 + 31 + 30 + 31 + 30 + 31,
  115. 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
  116. 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
  117. 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
  118. 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
  119. };
  120. time_t mktime(struct time *tm) {
  121. schk_init();
  122. time_t res;
  123. int year;
  124. year = tm->year - 1970;
  125. /* this not includes this year */
  126. res = year * YEAR + ((year + 1) >> 2) * DAY;
  127. /* if this year isn't a leap-year, and current-month > 2 */
  128. /* month starts at 0 */
  129. if (!isleapyear(tm->year) && tm->month > 1)
  130. res -= DAY;
  131. res += month[tm->month] * DAY;
  132. res += (tm->day - 1) * DAY;
  133. res += tm->hour * HOUR;
  134. res += tm->min * MIN;
  135. res += tm->sec;
  136. schk_leave();
  137. return res;
  138. }