Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add drive device #555

Merged
merged 13 commits into from
Mar 11, 2024
Merged

Add drive device #555

merged 13 commits into from
Mar 11, 2024

Conversation

vinc
Copy link
Owner

@vinc vinc commented Nov 30, 2023

  • Add fs::DeviceType::Drive
  • Implement FileIO::read for ata::Drive
  • Implement FileIO::write for ata::Drive
  • Store bus number and drive number after the device type byte in the device file
  • Create device from its device file's whole buffer instead of only the first byte
  • Create /dev/ata/<bus>/<dsk> to read the drive device
  • Add SEEK syscall
  • Update disk usage command to read superblock from drive device
  • Update disk erase command to write 0 to the whole drive device
  • Update disk format to write 0 to the block bitmaps, create a new superblock, and create the root dir
  • Update disk list to read more information from the drive device (name, size)

@vinc
Copy link
Owner Author

vinc commented Nov 30, 2023

With hex we can read the first block of the drive (which contains the beginning of the bootloader):

~
> hex /dev/ata/0/0
00000000: 31C0 8ED8 8EC0 8ED0 8EE0 8EE8 FCBC 007C 1..............|
00000010: BE12 7D66 E8A2 0000 00E4 92A8 0275 060C ..}f.........u..
00000020: 0224 FEE6 92FA 1E06 0F01 1676 7D0F 20C0 .$.........v}. .
00000030: 0C01 0F22 C0EB 00BB 1000 8EDB 8EC3 24FE ..."..........$.
00000040: 0F22 C007 1FFB BB01 0F66 B800 8F0B 003E .".......f.....>
00000050: 6789 18B4 41BB AA55 CD13 0F82 AA00 66B8 g...A..U......f.
00000060: 007E 0000 6689 C366 C1EB 0489 1E98 7D66 .~..f..f......}f
00000070: C1E3 0466 29D8 A396 7D66 B800 7E00 0066 ...f)...}f..~..f
00000080: BB00 6201 0066 29C3 66C1 EB09 891E 947D ..b..f).f......}
00000090: 66BB 007C 0000 6629 D866 C1E8 0966 A39A f..|..f).f...f..
000000A0: 7DBE 927D B442 CD13 7263 C706 987D 0000 }..}.B..rc...}..
000000B0: 66B8 457E 0000 66FF E0EB FE66 E80C 0000 f.E~..f....f....
000000C0: 00B0 0D66 E814 0000 00B0 0AEB 10FC AC84 ...f............
000000D0: C074 0866 E804 0000 00EB F366 C3B4 0ECD .t.f.......f....
000000E0: 1066 C3B9 0400 88F8 C0E8 043C 0A72 0204 .f.........<.r..
000000F0: 0704 3066 E8E4 FFFF FFC1 E304 E2E8 66C3 ..0f..........f.
00000100: 66E8 B5FF FFFF EBB1 BE33 7DEB F3BE 547D f........3}...T}
00000110: EBEE 426F 6F74 696E 6720 2866 6972 7374 ..Booting (first
00000120: 2073 7461 6765 292E 2E2E 0045 7272 6F72  stage)....Error
00000130: 3A20 004E 6F20 7375 7070 6F72 7420 666F : .No support fo
00000140: 7220 696E 7431 3368 2065 7874 656E 7369 r int13h extensi
00000150: 6F6E 7300 4661 696C 6564 2074 6F20 6C6F ons.Failed to lo
00000160: 6164 2072 6573 7420 6F66 2062 6F6F 746C ad rest of bootl
00000170: 6F61 6465 7200 1700 7A7D 0000 0000 0000 oader...z}......
00000180: 0000 FFFF 0000 009A CF00 FFFF 0000 0092 ................
00000190: CF00 1000 0000 0000 0000 0000 0000 0000 ................
000001A0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
000001B0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
000001C0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
000001D0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
000001E0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
000001F0: 0000 0000 0000 0000 0000 0000 0000 55AA ..............U.

And if we don't close the file handle we can read the subsequent blocks, for example in Lisp:

> (load "/lib/lisp/core.lsp")
true

> (var file (file.open "/dev/ata/0/0" "r"))
file

> (file.read file 512)
(49 192 142 216 142 192 142 208 142 224 142 232 252 188 0 124 190 18 125 102 232 162 0 0 0 228 146 168 2 117 6 12 2 36 254 230 146 250 30 6 15 1 22 118 125 15 32 192 12 1 15 34 192 235 0 187 16 0 142 219 142 195 36 254 15 34 192 7 31 251 187 1 15 102 184 0 143 11 0 62 103 137 24 180 65 187 170 85 205 19 15 130 170 0 102 184 0 126 0 0 102 137 195 102 193 235 4 137 30 152 125 102 193 227 4 102 41 216 163 150 125 102 184 0 126 0 0 102 187 0 98 1 0 102 41 195 102 193 235 9 137 30 148 125 102 187 0 124 0 0 102 41 216 102 193 232 9 102 163 154 125 190 146 125 180 66 205 19 114 99 199 6 152 125 0 0 102 184 69 126 0 0 102 255 224 235 254 102 232 12 0 0 0 176 13 102 232 20 0 0 0 176 10 235 16 252 172 132 192 116 8 102 232 4 0 0 0 235 243 102 195 180 14 205 16 102 195 185 4 0 136 248 192 232 4 60 10 114 2 4 7 4 48 102 232 228 255 255 255 193 227 4 226 232 102 195 102 232 181 255 255 255 235 177 190 51 125 235 243 190 84 125 235 238 66 111 111 116 105 110 103 32 40 102 105 114 115 116 32 115 116 97 103 101 41 46 46 46 0 69 114 114 111 114 58 32 0 78 111 32 115 117 112 112 111 114 116 32 102 111 114 32 105 110 116 49 51 104 32 101 120 116 101 110 115 105 111 110 115 0 70 97 105 108 101 100 32 116 111 32 108 111 97 100 32 114 101 115 116 32 111 102 32 98 111 111 116 108 111 97 100 101 114 0 23 0 122 125 0 0 0 0 0 0 0 0 255 255 0 0 0 154 207 0 255 255 0 0 0 146 207 0 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 85 170)

> (file.read file 512)
(66 111 111 116 105 110 103 32 40 115 101 99 111 110 100 32 115 116 97 103 101 41 46 46 46 0 70 97 105 108 101 100 32 116 111 32 108 111 97 100 32 107 101 114 110 101 108 32 102 114 111 109 32 100 105 115 107 0 190 26 126 102 232 120 254 255 255 235 254 190 0 126 102 232 109 254 255 255 156 184 0 236 179 2 205 21 157 102 184 0 5 0 0 163 150 125 199 6 148 125 1 0 102 184 0 98 1 0 102 187 0 124 0 0 102 41 216 102 193 232 9 102 163 154 125 102 191 0 0 64 0 102 185 0 3 29 0 102 129 193 255 1 0 0 102 193 233 9 190 146 125 180 66 205 19 114 157 102 81 102 86 102 185 128 0 0 0 102 15 183 54 150 125 243 103 102 165 102 94 102 89 102 161 154 125 102 131 192 1 102 163 154 125 102 131 233 1 117 205 38 141 62 0 80 102 232 34 0 0 0 102 232 129 2 0 0 250 15 1 22 118 125 15 32 192 12 1 15 34 192 106 8 102 184 120 127 0 0 102 80 102 203 235 254 102 49 219 49 237 102 186 80 65 77 83 102 184 32 232 0 0 38 102 199 69 20 1 0 0 0 102 185 24 0 0 0 205 21 114 91 102 186 80 65 77 83 102 57 208 117 80 102 133 219 116 75 235 31 102 184 32 232 0 0 38 102 199 69 20 1 0 0 0 102 185 24 0 0 0 205 21 114 41 102 186 80 65 77 83 227 28 128 249 20 118 7 38 246 69 20 1 116 16 38 102 139 77 8 38 102 11 77 12 116 4 69 131 199 24 102 133 219 117 190 137 46 118 127 248 102 195 249 102 195 0 0 102 187 16 0 102 142 219 102 142 195 102 142 211 102 190 248 128 232 244 1 0 0 232 244 0 0 0 232 16 1 0 0 250 15 1 29 212 128 0 0 191 0 16 0 0 185 0 80 0 0 41 249 193 233 2 49 192 243 171 184 0 32 0 0 131 200 3 163 0 16 0 0 184 0 48 0 0 131 200 3 163 0 32 0 0 184 0 64 0 0 131 200 3 163 0 48 0 0 184 131 0 64 0 185 2 0 0 0 186 0 3 29 0 129 194 0 0 64 0 129 194 255 255 31 0 193 234 21 137 4 205 0 48 0 0 5)

> (file.read file 512)
(0 0 32 0 131 193 1 57 209 114 237 184 0 16 0 0 37 0 240 255 255 131 200 3 185 0 16 0 0 193 233 12 186 0 98 1 0 129 194 255 15 0 0 193 234 12 137 4 205 0 64 0 0 5 0 16 0 0 131 193 1 57 209 114 237 232 27 1 0 0 15 9 15 174 240 184 0 16 0 0 15 34 216 15 32 224 131 200 32 15 34 224 185 128 0 0 192 15 50 13 0 1 0 0 15 48 15 32 192 13 0 0 0 128 15 34 192 15 1 21 242 128 0 0 106 8 184 192 131 0 0 80 203 235 254 156 88 137 193 53 0 0 32 0 80 157 156 88 81 157 57 200 116 1 195 190 17 129 0 0 232 221 0 0 0 244 235 253 184 0 0 0 128 15 162 61 1 0 0 128 114 16 184 1 0 0 128 15 162 247 194 0 0 0 32 116 1 195 190 51 129 0 0 232 178 0 0 0 244 235 253 144 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 154 32 0 0 0 0 0 0 146 0 0 144 0 0 26 0 215 128 0 0 66 111 111 116 105 110 103 32 40 116 104 105 114 100 32 115 116 97 103 101 41 46 46 46 0 69 114 114 111 114 58 32 67 80 85 32 100 111 101 115 32 110 111 116 32 115 117 112 112 111 114 116 32 67 80 85 73 68 0 69 114 114 111 114 58 32 67 80 85 32 100 111 101 115 32 110 111 116 32 115 117 112 112 111 114 116 32 108 111 110 103 32 109 111 100 101 0 180 0 176 3 205 16 102 195 184 0 0 10 0 131 200 3 137 193 193 233 12 137 4 205 0 64 0 0 5 0 16 0 0 61 0 0 12 0 124 232 195 80 83 81 82 232 32 0 0 0 186 0 0 0 0 161 210 129 0 0 185 160 0 0 0 247 241 131 192 1 247 225 163 210 129 0 0 90 89 91 88 195 252 172 132 192 116 7 232 3 0 0 0 235 244 195 139 29 210 129 0 0 180 15 102 137 131 0 128 11 0 131 195 2 137 29 210 129 0 0 195 0 0 0 0 0 0 0 0 204 204 204 204 204 204 195 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 72 131 236 56 72 137 248 72 193 232 52 72 137 4 36 117)

But there is currently no way to pass the context needed to the device to know which ATA bus and drive we want to read. In the current implementation we hardcoded the first drive of the first bus.

Currently when we create a device we create a special dir entry, and then we write the number representing the device kind in the disk, so that when we later open a device we can get its kind by reading the file.

We could do more than storing just a number in this file. We could also store the context there.

@vinc
Copy link
Owner Author

vinc commented Nov 30, 2023

We can now store specific device information when we create them that can be used when opening them.

@vinc
Copy link
Owner Author

vinc commented Dec 1, 2023

Currently sys::fs::file::File::open() doesn't allow to open the file representing a device on the file system. We could allow that to get information on the device from userspace. The first byte of this file would give us the device type and that could be used to improve usr::read.

@vinc
Copy link
Owner Author

vinc commented Dec 1, 2023

Now that we can directly read a drive in userspace we could read its superblock if it exists to get the number of blocks available and the number of blocks used for usr::disk::usage() instead of relying on sys::fs::disk_size() and sys::fs::disk_used().

And after implementing writes we could also stay in userspace for usr::disk::erase() instead of using sys::ata::write().

Finally for usr::disk::list we could add more information about the drive in its device file.

@vinc
Copy link
Owner Author

vinc commented Dec 1, 2023

Example of Lisp code to read file:

(load "/lib/lisp/core.lsp")

(var path (first args))
(var file (file.open path "r"))
(var buf (file.read file 512))
(var stdout 1)
(while (> (len buf) 0) (do
  (file.write stdout buf)
  (set buf (file.read file 512))))

@vinc
Copy link
Owner Author

vinc commented Mar 11, 2024

This PR is not finished but it is already useful as it is so we can merge it and open another one to finish the work later.

@vinc vinc marked this pull request as ready for review March 11, 2024 11:28
@vinc vinc merged commit 6f1de4e into trunk Mar 11, 2024
1 check passed
@vinc vinc deleted the feature/drive-device branch March 11, 2024 11:29
@vinc vinc mentioned this pull request Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant