‫Disk access and how it works.
‫The very first thing you need to know about Desk's is the files do not actually exist on them.
‫Files are a colonel operating system concept on disks.
‫You have what's known as filesystems.
‫They are special data structures that explain files.
‫It is up to the colonel to read these data structures correctly.
‫The disk itself has no concept of files.
‫The disk itself just holds loads of data blocks called sectors.
‫If you want your colonel to understand files, you have to create a file system driver that targets
‫that specific file system that is on your desk.
‫You know, when you plug in a new USB stick into your computer and it gives you the option to format
‫it and ask which file system you want while Windows or Linux, whatever you use, it knows how to read
‫that file system that your formatting it to.
‫So data is typically written in sectors.
‫So for example, we would write five to 12 bytes to sector three, something like that.
‫Right.
‫We read 512 bytes from sector three.
‫This is how it works right now.
‫The sector sizes can be different depending on the disk, although all modern hard disk generally five,
‫10, 12 bytes per sector.
‫However, if you were reading or writing to a CD, then the block size, the sector size is generally
‫bigger.
‫So if we were to read Sector three, for example, the disk would return five, 10, 12 bytes of data
‫for that sector.
‫We can then read that data into memory.
‫So the old style of reading disks is called C.
‫S Cylinder head sector.
‫And how this works is sectors are read and written by specifying a head track and a sector.
‫So you actually have to select where in the disk you want to read as shown in this diagram here.
‫So this is an old fashioned way of doing it and it isn't the modern way.
‫And it's generally more complicated because you need to know things such as how many sectors there are,
‫how many cylinders, how many tracks and how many heads.
‫And it can make things pretty complicated.
‫Right?
‫So C is the old way and we will do a bit of C.
‫S, but I don't advise using it for the long term.
‫So we'll switch to something known as LBA later down the course, which is the modern way of reading
‫from the disk.
‫So LBA logical block address.
‫This is the modern way of reading from a hard disk.
‫So rather than specifying head track and sector, we just specify a number that starts from zero.
‫So for example, if we read LBA zero, that would return the first sector on the disk to us.
‫So LBA allows us to read from the disc as if we're reading blocks from a very large file.
‫So let's say we had a 20 gigabyte file, right?
‫You might do something like Elfriede and you might specify the size is 512, and then you might put
‫like six for the four for the block you want to read.
‫Right.
‫This is exactly how it works with LBA.
‫Basically, apart from we have to read from the disk in sectors, we can't say I want to read by five
‫five eight three two in the disk.
‫We can't do that.
‫We have to first calculate what the sector numbers for that offset and then we need to read that sector
‫and then offset from there to get the buy we were looking for.
‫But I'll explain the math behind that very soon.
‫So LBA zero is the first sector on the disk.
‫LBA one is the second sector on the disk and so on.
‫So, you know, these two together make up 1024 bytes, for example.
‫OK, so we're going to discuss calculating, logical blockages now.
‫So let's say we want to read the byte opposition five eight three seven six on the disk.
‫So how do we do it while we get our LBA, which is five eight three seven six divided by five, 10,
‫12, five, 10, 12 being our sector size.
‫Like I said, all more than hard disks is generally five to 12 bytes prosector.
‫So that gives us LBA.
‫But we're not done there because we now need to get the offset because it might not divide perfectly
‫into five 12.
‫Right.
‫So we read that LBA and we load 512 bytes into memory.
‫So now we need to know the offset that our byte is in in our buffer.
‫Right.
‫Because we just loaded five, 10, 12 bytes into our buffer.
‫We now need to find out where the bipartisan above is so we can use the remainder operator to do this.
‫So we go five eight three seven six remainder five, 12.
‫That gives us eight eight bytes into our buffer.
‫That is where that byte is.
‫So let's just calculate that math is correct.
‫So we do one one four, which is the ALBAR.
‫We calculated times the sector size, which gives us fifty eight thousand three hundred seventy three
‫hundred and sixty eight.
‫And then we add to that number, which gives us the number we were trying to read in the first place.
‫So you can see loading from loading data from blocks isn't that difficult, right?
‫You just need to use a bit of math and be a bit clever about it.
‫So was where in 16 real mode, the BIOS provides Interop 13 for disk operations.
‫So we don't actually have to write our own driver for this.
‫We just ask the bios to read read for us.
‫However, in thirty two bit protective mode, you have to create your own disk.
‫Stryver, which gets a little more complicated, but you don't have to worry about that right now.
‫We're still very early in the course.