1
0

diskfs.g 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. const DISKFD_DESTROY 0
  15. const DISKFD_READ 4
  16. const DISKFD_WRITE 8
  17. const DISKFD_TRUNCATE 12
  18. const DISKFD_SEEK 16
  19. const DISKFD_POS 20
  20. const DISKFD_ATAPIO 24
  21. const DISKFD_SIZE 28
  22. const DISKFD_START 32
  23. const DISKFD_CACHE 36
  24. const SIZEOF_DISKFD 40
  25. fun diskfd_destroy 1 {
  26. $fd
  27. @fd 0 param = ;
  28. fd DISKFD_CACHE take free ;
  29. fd free ;
  30. }
  31. fun diskfd_read 1 {
  32. $fd
  33. @fd 0 param = ;
  34. fd DISKFD_POS take fd DISKFD_SIZE take <= "diskfd_read: invalid read position" assert_msg ;
  35. if fd DISKFD_POS take fd DISKFD_SIZE take == {
  36. 0xffffffff ret ;
  37. }
  38. $pos
  39. @pos fd DISKFD_POS take fd DISKFD_START take + = ;
  40. $sect_pos
  41. @sect_pos pos 512 % = ;
  42. $cache
  43. @cache fd DISKFD_CACHE take = ;
  44. if cache 0 == {
  45. $sect_num
  46. @sect_num pos 512 / = ;
  47. $atapio
  48. @atapio fd DISKFD_ATAPIO take = ;
  49. @cache atapio sect_num atapio_read_sect = ;
  50. fd DISKFD_CACHE take_addr cache = ;
  51. }
  52. $c
  53. @c cache sect_pos + **c = ;
  54. if sect_pos 511 == {
  55. cache free ;
  56. fd DISKFD_CACHE take_addr 0 = ;
  57. }
  58. fd DISKFD_POS take_addr fd DISKFD_POS take 1 + = ;
  59. # "Read char " log ;
  60. # c itoa log ;
  61. # "\n" log ;
  62. c ret ;
  63. }
  64. fun diskfd_write 2 {
  65. $fd
  66. $c
  67. @fd 1 param = ;
  68. @c 0 param = ;
  69. 0 "diskfd_write: not supported" assert_msg ;
  70. }
  71. fun diskfd_truncate 1 {
  72. $fd
  73. @fd 0 param = ;
  74. 0 "diskfd_truncate: not supported" assert_msg ;
  75. }
  76. fun diskfd_seek 3 {
  77. $fd
  78. $off
  79. $whence
  80. @fd 2 param = ;
  81. @off 1 param = ;
  82. @whence 0 param = ;
  83. $size
  84. @size fd DISKFD_SIZE take = ;
  85. if whence 1 == {
  86. @off off fd DISKFD_POS take + = ;
  87. }
  88. if whence 2 == {
  89. @off off size + = ;
  90. }
  91. if off size > {
  92. @off size = ;
  93. }
  94. fd DISKFD_POS take_addr off = ;
  95. fd DISKFD_CACHE take free ;
  96. fd DISKFD_CACHE take_addr 0 = ;
  97. off ret ;
  98. }
  99. fun diskfd_init 3 {
  100. $atapio
  101. $start
  102. $size
  103. @atapio 2 param = ;
  104. @start 1 param = ;
  105. @size 0 param = ;
  106. $fd
  107. @fd SIZEOF_DISKFD malloc = ;
  108. fd DISKFD_DESTROY take_addr @diskfd_destroy = ;
  109. fd DISKFD_READ take_addr @diskfd_read = ;
  110. fd DISKFD_WRITE take_addr @diskfd_write = ;
  111. fd DISKFD_TRUNCATE take_addr @diskfd_truncate = ;
  112. fd DISKFD_SEEK take_addr @diskfd_seek = ;
  113. fd DISKFD_POS take_addr 0 = ;
  114. fd DISKFD_ATAPIO take_addr atapio = ;
  115. fd DISKFD_START take_addr start = ;
  116. fd DISKFD_SIZE take_addr size = ;
  117. fd DISKFD_CACHE take_addr 0 = ;
  118. fd ret ;
  119. }
  120. const DISKMOUNT_DESTROY 0
  121. const DISKMOUNT_OPEN 4
  122. const DISKMOUNT_ATAPIO 8
  123. const DISKMOUNT_STARTS 12
  124. const DISKMOUNT_SIZES 16
  125. const SIZEOF_DISKMOUNT 20
  126. fun diskmount_destroy 1 {
  127. $disk
  128. @disk 0 param = ;
  129. disk DISKMOUNT_STARTS take map_destroy ;
  130. disk DISKMOUNT_SIZES take map_destroy ;
  131. disk DISKMOUNT_ATAPIO take atapio_destroy ;
  132. disk free ;
  133. }
  134. fun diskmount_open 2 {
  135. $disk
  136. $name
  137. @disk 1 param = ;
  138. @name 0 param = ;
  139. if disk DISKMOUNT_STARTS take name map_has ! {
  140. 0 ret ;
  141. }
  142. disk DISKMOUNT_SIZES take name map_has "diskmount_open: error 1" assert_msg ;
  143. disk DISKMOUNT_ATAPIO take disk DISKMOUNT_STARTS take name map_at disk DISKMOUNT_SIZES take name map_at diskfd_init ret ;
  144. }
  145. fun diskmount_init 2 {
  146. $atapio
  147. $start
  148. @atapio 1 param = ;
  149. @start 0 param = ;
  150. $disk
  151. @disk SIZEOF_DISKMOUNT malloc = ;
  152. disk DISKMOUNT_DESTROY take_addr @diskmount_destroy = ;
  153. disk DISKMOUNT_OPEN take_addr @diskmount_open = ;
  154. disk DISKMOUNT_ATAPIO take_addr atapio = ;
  155. disk DISKMOUNT_STARTS take_addr map_init = ;
  156. disk DISKMOUNT_SIZES take_addr map_init = ;
  157. # Parse the file table
  158. $fd
  159. @fd atapio start 0x7fffffff diskfd_init = ;
  160. # Discard the first eight byte (magic number)
  161. $i
  162. @i 0 = ;
  163. while i 8 < {
  164. fd diskfd_read ;
  165. @i i 1 + = ;
  166. }
  167. $cont
  168. @cont 1 = ;
  169. while cont {
  170. # Scan the filename
  171. $filename
  172. $cap
  173. $len
  174. @cap 10 = ;
  175. @len 0 = ;
  176. @filename cap malloc = ;
  177. $cont2
  178. @cont2 1 = ;
  179. while cont2 {
  180. if len cap == {
  181. @cap cap 2 * = ;
  182. @filename cap filename realloc = ;
  183. }
  184. len cap < "diskmount_init: error 1" assert_msg ;
  185. $c
  186. @c fd diskfd_read =c ;
  187. filename len + c =c ;
  188. if c 0 == {
  189. @cont2 0 = ;
  190. } else {
  191. @len len 1 + = ;
  192. }
  193. }
  194. if len 0 == {
  195. filename free ;
  196. @cont 0 = ;
  197. } else {
  198. # Read the position
  199. $pos
  200. @pos fd diskfd_read = ;
  201. @pos pos 8 << fd diskfd_read + = ;
  202. @pos pos 8 << fd diskfd_read + = ;
  203. @pos pos 8 << fd diskfd_read + = ;
  204. # Read the size
  205. $size
  206. @size fd diskfd_read = ;
  207. @size size 8 << fd diskfd_read + = ;
  208. @size size 8 << fd diskfd_read + = ;
  209. @size size 8 << fd diskfd_read + = ;
  210. # Store data in tables
  211. disk DISKMOUNT_STARTS take filename map_has ! "diskmount_init: duplicated file name" assert_msg ;
  212. disk DISKMOUNT_SIZES take filename map_has ! "diskmount_init: error 2" assert_msg ;
  213. disk DISKMOUNT_STARTS take filename start pos + map_set ;
  214. disk DISKMOUNT_SIZES take filename size map_set ;
  215. disk DISKMOUNT_STARTS take filename map_has "diskmount_init: error 3" assert_msg ;
  216. disk DISKMOUNT_SIZES take filename map_has "diskmount_init: error 4" assert_msg ;
  217. # Debug log
  218. # "File " log ;
  219. # filename log ;
  220. # " at position " log ;
  221. # pos itoa log ;
  222. # " of size " log ;
  223. # size itoa log ;
  224. # "\n" log ;
  225. filename free ;
  226. }
  227. }
  228. fd diskfd_destroy ;
  229. disk ret ;
  230. }