vfs.g 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 SEEK_SET 0
  15. const SEEK_CUR 1
  16. const SEEK_END 2
  17. const FD_DESTROY 0
  18. const FD_READ 4
  19. const FD_WRITE 8
  20. const FD_TRUNCATE 12
  21. const FD_SEEK 16
  22. const MOUNT_DESTROY 0
  23. const MOUNT_OPEN 4
  24. const INITFD_DESTROY 0
  25. const INITFD_READ 4
  26. const INITFD_WRITE 8
  27. const INITFD_TRUNCATE 12
  28. const INITFD_SEEK 16
  29. const INITFD_START 20
  30. const INITFD_CUR 24
  31. const INITFD_END 28
  32. const SIZEOF_INITFD 32
  33. fun initfd_destroy 1 {
  34. $fd
  35. @fd 0 param = ;
  36. fd free ;
  37. }
  38. fun initfd_read 1 {
  39. $fd
  40. @fd 0 param = ;
  41. if fd INITFD_CUR take fd INITFD_END take == {
  42. 0xffffffff ret ;
  43. } else {
  44. $c
  45. @c fd INITFD_CUR take **c = ;
  46. fd INITFD_CUR take_addr fd INITFD_CUR take 1 + = ;
  47. c ret ;
  48. }
  49. }
  50. fun initfd_write 2 {
  51. 0 "initfd_write: not supported" assert_msg ;
  52. }
  53. fun initfd_truncate 1 {
  54. 0 "initfd_truncate: not supported" assert_msg ;
  55. }
  56. fun initfd_seek 3 {
  57. $fd
  58. $off
  59. $whence
  60. @fd 2 param = ;
  61. @off 1 param = ;
  62. @whence 0 param = ;
  63. if whence SEEK_SET == off 0 == && {
  64. fd INITFD_CUR take_addr fd INITFD_START take = ;
  65. 0 ret ;
  66. }
  67. 0 "initfd_seek: unsupported seek" assert_msg ;
  68. }
  69. fun initfd_init 1 {
  70. $name
  71. @name 0 param = ;
  72. $begin
  73. $end
  74. @begin @end name platform_walk_initrd ;
  75. if begin 0 == {
  76. 0 ret ;
  77. }
  78. $fd
  79. @fd SIZEOF_INITFD malloc = ;
  80. fd INITFD_DESTROY take_addr @initfd_destroy = ;
  81. fd INITFD_READ take_addr @initfd_read = ;
  82. fd INITFD_WRITE take_addr @initfd_write = ;
  83. fd INITFD_TRUNCATE take_addr @initfd_truncate = ;
  84. fd INITFD_SEEK take_addr @initfd_seek = ;
  85. fd INITFD_START take_addr begin = ;
  86. fd INITFD_CUR take_addr begin = ;
  87. fd INITFD_END take_addr end = ;
  88. fd ret ;
  89. }
  90. const INITMOUNT_DESTROY 0
  91. const INITMOUNT_OPEN 4
  92. const SIZEOF_INITMOUNT 8
  93. fun initmount_destroy 1 {
  94. $mount
  95. @mount 0 param = ;
  96. mount free ;
  97. }
  98. fun initmount_open 2 {
  99. $mount
  100. $name
  101. @mount 1 param = ;
  102. @name 0 param = ;
  103. name initfd_init ret ;
  104. }
  105. fun initmount_init 0 {
  106. $mount
  107. @mount SIZEOF_INITMOUNT malloc = ;
  108. mount INITMOUNT_DESTROY take_addr @initmount_destroy = ;
  109. mount INITMOUNT_OPEN take_addr @initmount_open = ;
  110. mount ret ;
  111. }
  112. const VFSINST_MOUNTS 0
  113. const SIZEOF_VFSINST 4
  114. fun vfsinst_init 0 {
  115. $vfsinst
  116. @vfsinst SIZEOF_VFSINST malloc = ;
  117. vfsinst VFSINST_MOUNTS take_addr map_init = ;
  118. vfsinst ret ;
  119. }
  120. fun mount_destroy_closure 3 {
  121. $ctx
  122. $key
  123. $value
  124. @ctx 2 param = ;
  125. @key 1 param = ;
  126. @value 0 param = ;
  127. value value MOUNT_DESTROY take \1 ;
  128. }
  129. fun vfsinst_destroy 1 {
  130. $vfsinst
  131. @vfsinst 0 param = ;
  132. $mounts
  133. @mounts vfsinst VFSINST_MOUNTS take = ;
  134. mounts @mount_destroy_closure 0 map_foreach ;
  135. mounts map_destroy ;
  136. vfsinst free ;
  137. }
  138. fun vfsinst_mount 3 {
  139. $vfsinst
  140. $point
  141. $mount
  142. @vfsinst 2 param = ;
  143. @point 1 param = ;
  144. @mount 0 param = ;
  145. "Mounting file system /" log ;
  146. point log ;
  147. "\n" log ;
  148. $mounts
  149. @mounts vfsinst VFSINST_MOUNTS take = ;
  150. mounts point mount map_set ;
  151. }
  152. fun find_slash 1 {
  153. $s
  154. @s 0 param = ;
  155. $t
  156. @t s = ;
  157. while 1 {
  158. $c
  159. @c t **c = ;
  160. if c 0 == c '/' == || {
  161. t s - ret ;
  162. }
  163. @t t 1 + = ;
  164. }
  165. }
  166. fun vfsinst_open 2 {
  167. $vfsinst
  168. $name
  169. @vfsinst 1 param = ;
  170. @name 0 param = ;
  171. name **c '/' == "vfsinst_open: missing initial slash" assert_msg ;
  172. @name name 1 + = ;
  173. $mountname
  174. @mountname name strdup = ;
  175. $slash_pos
  176. @slash_pos mountname find_slash = ;
  177. mountname slash_pos + **c '/' == "vfsinst_open: missing mountpoint slash" assert_msg ;
  178. mountname slash_pos + 0 =c ;
  179. $mountpath
  180. @mountpath mountname slash_pos + 1 + = ;
  181. $mount
  182. mount vfsinst VFSINST_MOUNTS take mountname map_has "vfsinst_open: mount point does not exist" assert_msg ;
  183. @mount vfsinst VFSINST_MOUNTS take mountname map_at = ;
  184. $res
  185. @res mount mountpath mount MOUNT_OPEN take \2 = ;
  186. mountname free ;
  187. res ret ;
  188. }
  189. $vfs
  190. fun vfs_init 0 {
  191. $compile_disk
  192. @compile_disk 0 param = ;
  193. @vfs vfsinst_init = ;
  194. vfs "ram" rammount_init vfsinst_mount ;
  195. vfs "init" initmount_init vfsinst_mount ;
  196. if compile_disk {
  197. # Symbol has to be retrieved indirectly, because it is not
  198. # available if compile_disk is false
  199. vfs 0 "mbr_vfs_scan" platform_get_symbol \1 ;
  200. }
  201. }
  202. fun vfs_destroy 0 {
  203. vfs vfsinst_destroy ;
  204. }
  205. fun vfs_open 1 {
  206. $name
  207. @name 0 param = ;
  208. vfs name vfsinst_open ret ;
  209. }
  210. fun vfs_close 1 {
  211. $fd
  212. @fd 0 param = ;
  213. fd fd FD_DESTROY take \1 ;
  214. }
  215. fun vfs_read 1 {
  216. $fd
  217. @fd 0 param = ;
  218. fd fd FD_READ take \1 ret ;
  219. }
  220. fun vfs_write 2 {
  221. $fd
  222. $c
  223. @fd 1 param = ;
  224. @c 0 param = ;
  225. fd c fd FD_WRITE take \1 ;
  226. }
  227. fun vfs_truncate 1 {
  228. $fd
  229. @fd 0 param = ;
  230. fd fd FD_TRUNCATE take \1 ;
  231. }
  232. fun vfs_seek 3 {
  233. $fd
  234. $off
  235. $whence
  236. @fd 2 param = ;
  237. @off 1 param = ;
  238. @whence 0 param = ;
  239. fd off whence fd FD_SEEK take \3 ret ;
  240. }
  241. fun vfs_reset 1 {
  242. $fd
  243. @fd 0 param = ;
  244. fd 0 SEEK_SET vfs_seek ;
  245. }