main.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <arch/io.h>
  35. #include <arch/sys.h>
  36. #include <arch/x86/int.h>
  37. #include <arch/x86/paging.h>
  38. /* we save boot-time arguments at that address where boot was. */
  39. struct bootarg *bootarg = (struct bootarg *) 0x7c00;
  40. void time_init(void);
  41. /* well, it looks like linux */
  42. #define CMOS_READ(addr) ({ \
  43. outb(addr, 0x70); \
  44. inb(0x71); \
  45. }) \
  46. /* only for byte */
  47. #define BCD2BIN(byte) ((byte) = ((byte) & 0xf) + ((byte) >> 4) * 10)
  48. void time_init(void);
  49. time_t mktime(struct time *);
  50. /*
  51. * init(void): initial all modules and devices.
  52. *
  53. * return value: error number. main will return after initial done.
  54. * if return value isn't 0, head.S will panic. else, it will halt.
  55. * */
  56. int init(void) {
  57. console_init();
  58. print(red, "XNIX kernel starting...\n");
  59. char buf[256] = {0};
  60. int_init();
  61. mm_init();
  62. tty_read(&stdin, buf, 256);
  63. task_init();
  64. kbd_init();
  65. sti();
  66. printk(white, "total %d devices. \n", pci_lspci());
  67. printk(white, "System boots from the device %b: ", bootarg->bootdev);
  68. if (bootarg->bootdev & 0x80)
  69. printk(white, "hard disk %b. \n", bootarg->bootdev & 7);
  70. else
  71. printk(white, "floppy / flash memory / cd-rom %b. \n", bootarg->bootdev);
  72. time_init();
  73. return 0;
  74. }
  75. void time_init(void) {
  76. /* get current time, and save it to `startup_time` */
  77. struct time tm;
  78. /* it will execute when 'sec' changed for the second time */
  79. do {
  80. tm.sec = CMOS_READ(0);
  81. tm.min = CMOS_READ(2);
  82. tm.hour = CMOS_READ(4);
  83. tm.day = CMOS_READ(7);
  84. /* mouth is 1 - 12 */
  85. tm.month = CMOS_READ(8) - 1;
  86. /* this is the year only 2 bits (1970 -> 70) */
  87. tm.year = CMOS_READ(9);
  88. } while (tm.sec != CMOS_READ(0));
  89. BCD2BIN(tm.sec);
  90. BCD2BIN(tm.min);
  91. BCD2BIN(tm.hour);
  92. BCD2BIN(tm.day);
  93. BCD2BIN(tm.year);
  94. tm.year += tm.year >= 70 ? 1900 : 2000;
  95. printk(white, "current time: %d %d %d %d:%d:%d. \n",
  96. tm.year, tm.month + 1, tm.day, tm.hour, tm.min, tm.sec);
  97. printk(white, "current unix time-stamp: %d. \n", mktime(&tm));
  98. return;
  99. }
  100. static int month[] = {
  101. 0,
  102. 31,
  103. 31 + 29,
  104. 31 + 29 + 31,
  105. 31 + 29 + 31 + 30,
  106. 31 + 29 + 31 + 30 + 31,
  107. 31 + 29 + 31 + 30 + 31 + 30,
  108. 31 + 29 + 31 + 30 + 31 + 30 + 31,
  109. 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
  110. 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
  111. 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
  112. 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
  113. };
  114. time_t mktime(struct time *tm) {
  115. time_t res;
  116. int year;
  117. year = tm->year - 1970;
  118. /* this not includes this year */
  119. res = year * YEAR + ((year + 1) >> 2) * DAY;
  120. /* if this year isn't a leap-year, and current-month > 2 */
  121. /* month starts at 0 */
  122. if (!isleapyear(tm->year) && tm->month > 1)
  123. res -= DAY;
  124. res += month[tm->month] * DAY;
  125. res += (tm->day - 1) * DAY;
  126. res += tm->hour * HOUR;
  127. res += tm->min * MIN;
  128. res += tm->sec;
  129. return res;
  130. }