main.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. serial_init();
  62. print(red, "\nXNIX kernel starting...\n");
  63. int_init();
  64. mm_init();
  65. task_init();
  66. kbd_init();
  67. sti();
  68. printk(white, "total %d devices. \n", pci_lspci());
  69. printk(white, "System boots from the device %b: ", bootarg->bootdev);
  70. if (bootarg->bootdev & 0x80)
  71. printk(white, "hard disk %b. \n", bootarg->bootdev & 7);
  72. else
  73. printk(white, "floppy / flash memory / cd-rom %b. \n", bootarg->bootdev);
  74. time_init();
  75. schk_leave();
  76. return 0;
  77. }
  78. void time_init(void) {
  79. schk_init();
  80. /* get current time, and save it to `startup_time` */
  81. struct time tm;
  82. /* it will execute when 'sec' changed for the second time */
  83. do {
  84. tm.sec = CMOS_READ(0);
  85. tm.min = CMOS_READ(2);
  86. tm.hour = CMOS_READ(4);
  87. tm.day = CMOS_READ(7);
  88. /* mouth is 1 - 12 */
  89. tm.month = CMOS_READ(8) - 1;
  90. /* this is the year only 2 bits (1970 -> 70) */
  91. tm.year = CMOS_READ(9);
  92. } while (tm.sec != CMOS_READ(0));
  93. BCD2BIN(tm.sec);
  94. BCD2BIN(tm.min);
  95. BCD2BIN(tm.hour);
  96. BCD2BIN(tm.day);
  97. BCD2BIN(tm.year);
  98. tm.year += tm.year >= 70 ? 1900 : 2000;
  99. printk(white, "current time: %d %d %d %d:%d:%d. \n",
  100. tm.year, tm.month + 1, tm.day, tm.hour, tm.min, tm.sec);
  101. printk(white, "current unix time-stamp: %d. \n", mktime(&tm));
  102. schk_leave();
  103. return;
  104. }
  105. static int month[] = {
  106. 0,
  107. 31,
  108. 31 + 29,
  109. 31 + 29 + 31,
  110. 31 + 29 + 31 + 30,
  111. 31 + 29 + 31 + 30 + 31,
  112. 31 + 29 + 31 + 30 + 31 + 30,
  113. 31 + 29 + 31 + 30 + 31 + 30 + 31,
  114. 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
  115. 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
  116. 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
  117. 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
  118. };
  119. time_t mktime(struct time *tm) {
  120. schk_init();
  121. time_t res;
  122. int year;
  123. year = tm->year - 1970;
  124. /* this not includes this year */
  125. res = year * YEAR + ((year + 1) >> 2) * DAY;
  126. /* if this year isn't a leap-year, and current-month > 2 */
  127. /* month starts at 0 */
  128. if (!isleapyear(tm->year) && tm->month > 1)
  129. res -= DAY;
  130. res += month[tm->month] * DAY;
  131. res += (tm->day - 1) * DAY;
  132. res += tm->hour * HOUR;
  133. res += tm->min * MIN;
  134. res += tm->sec;
  135. schk_leave();
  136. return res;
  137. }