1
0

vfs.g 5.0 KB

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