mbr.g 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # This file is part of asmc, a bootstrapping OS with minimal seed
  2. # Copyright (C) 2018 Giovanni Mascellani <gio@debian.org>
  3. # https://gitlab.com/giomasce/asmc
  4. # This program 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) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. fun read_mbr 1 {
  15. $atapio
  16. @atapio 0 param = ;
  17. # Read MBR
  18. $mbr
  19. @mbr atapio 0 atapio_read_sect = ;
  20. # Check if this is an actual MBR
  21. $res
  22. if mbr 510 + **c 0x55 == mbr 511 + **c 0xaa == && {
  23. @res 8 vector_init = ;
  24. $i
  25. @i 0 = ;
  26. while i 4 < {
  27. $start
  28. $size
  29. @start mbr 0x1be + i 16 * + 8 + ** = ;
  30. @size mbr 0x1be + i 16 * + 12 + ** = ;
  31. if start 0 != {
  32. $idx
  33. @idx res vector_size = ;
  34. res start vector_push_back ;
  35. res idx vector_at_addr 4 + size = ;
  36. }
  37. @i i 1 + = ;
  38. }
  39. } else {
  40. @res 0 = ;
  41. }
  42. mbr free ;
  43. res ret ;
  44. }
  45. fun mbr_vfs_scan_drive 3 {
  46. $vfs
  47. $base
  48. $master
  49. @vfs 2 param = ;
  50. @base 1 param = ;
  51. @master 0 param = ;
  52. $count
  53. @count 1 = ;
  54. $a
  55. @a base master atapio_init = ;
  56. if a atapio_print_identify {
  57. $parts
  58. @parts a read_mbr = ;
  59. if parts 0 != {
  60. "Found MBR!\n" 1 platform_log ;
  61. $i
  62. @i 0 = ;
  63. while i parts vector_size < {
  64. " Partition starting at LBA " 1 platform_log ;
  65. parts i vector_at itoa 1 platform_log ;
  66. " of size " 1 platform_log ;
  67. parts i vector_at_addr 4 + ** itoa 1 platform_log ;
  68. "\n" 1 platform_log ;
  69. # Read the first sector to see if there is a known file system
  70. $sect
  71. @sect a parts i vector_at atapio_read_sect = ;
  72. if sect ** "DISK" ** == sect 4 + ** "FS " ** == && {
  73. " Found a diskfs file system!\n" 1 platform_log ;
  74. $mount
  75. @mount a atapio_duplicate 512 parts i vector_at * diskmount_init = ;
  76. $point
  77. @point "disk" strdup count itoa append_to_str = ;
  78. vfs point mount 0 "vfsinst_mount" platform_get_symbol \3 ;
  79. point free ;
  80. @count count 1 + = ;
  81. } else {
  82. if sect ** "DEBU" ** == sect 4 + ** "GFS " ** == && {
  83. " Found a debugfs file system!\n" 1 platform_log ;
  84. $debugfs
  85. @debugfs a atapio_duplicate parts i vector_at parts i vector_at_addr 4 + ** debugfsinst_init = ;
  86. debugfs debugfs_set ;
  87. }
  88. }
  89. sect free ;
  90. @i i 1 + = ;
  91. }
  92. parts vector_destroy ;
  93. } else {
  94. "No MBR found...\n" 1 platform_log ;
  95. }
  96. }
  97. a atapio_destroy ;
  98. }
  99. fun mbr_vfs_scan 1 {
  100. $vfs
  101. @vfs 0 param = ;
  102. vfs 0x1f0 1 mbr_vfs_scan_drive ;
  103. vfs 0x1f0 0 mbr_vfs_scan_drive ;
  104. # vfs 0x170 1 mbr_vfs_scan_drive ;
  105. # vfs 0x170 0 mbr_vfs_scan_drive ;
  106. }
  107. fun mbr_read_test_drive 2 {
  108. $base
  109. $master
  110. @base 1 param = ;
  111. @master 0 param = ;
  112. $a
  113. @a base master atapio_init = ;
  114. if a atapio_print_identify {
  115. $parts
  116. @parts a read_mbr = ;
  117. $i
  118. @i 0 = ;
  119. while i parts vector_size < {
  120. "Partition starting at LBA " 1 platform_log ;
  121. parts i vector_at itoa 1 platform_log ;
  122. " of size " 1 platform_log ;
  123. parts i vector_at_addr 4 + ** itoa 1 platform_log ;
  124. "\n" 1 platform_log ;
  125. @i i 1 + = ;
  126. }
  127. parts vector_destroy ;
  128. }
  129. a atapio_destroy ;
  130. }
  131. fun mbr_read_test 0 {
  132. 0x1f0 1 mbr_read_test_drive ;
  133. 0x1f0 0 mbr_read_test_drive ;
  134. # 0x170 1 mbr_read_test_drive ;
  135. # 0x170 0 mbr_read_test_drive ;
  136. }