How does my computer/hdd know when a sector begins, an when a sector ends?
Start with floppies, since they are simpler and have tracks and sectors like hard drives.
All disk drives function with these capabilities:
- turn motor on/off
- move head to a certain track somehow
- head read mode: generate a signal when it runs over a flux change, wherever it is on the disk.
- head write mode: generate a flux change wherever it is on the disk.
How to detect when a sector starts?
There wasn't a single method, until 3.5" disks and the IBM PC became ubiquitous and created a de facto standard.
Here's an excerpt from the Wikipedia article on floppy discs:
All 8 inch and some 5 1⁄4-inch drives used a mechanical method to locate sectors, known as either hard sectors or soft sectors, and is
the purpose of the small hole in the jacket, off to the side of the
spindle hole. A light beam sensor detects when a punched hole in the
disk is visible through the hole in the jacket.
For a soft-sectored disk, there is only a single hole, which is used
to locate the first sector of each track. Clock timing is then used to
find the other sectors behind it, which requires precise speed
regulation of the drive motor.
For a hard-sectored disk, there are many holes, one for each sector
row, plus an additional hole in a half-sector position, that is used
to indicate sector zero.
The Apple II computer system is notable in that it did not have an
index hole sensor and ignored the presence of hard or soft sectoring.
Instead it used special repeating data synchronization patterns
written to the disk between each sector, to assist the computer in
finding and synchronizing with the data in each track.
Apple II is a well-known 8-bit computer that was first sold in 1978 and predates the IBM PC era. The Apple II is famous for doing as much in software as possible and with as few chips as possible, which resulted in a system where the CPU had to control the floppy disk head and reading/writing data from it on a low-level, and where luxuries like a chip to detect holes in the disk weren't used. The hardware connected to the disk is primitive and expected to be mostly driven by the CPU.
On Apple II you could not read/write the disk and run programs at the same time (you couldn't do this on most CP/M machines of that era either, also 8-bit machines).
So how exactly did that old Apple II read and write to a disk? More than you probably every wanted to know about those gory details are right here starting at chapter 3. It's getting heavy into timing patterns and such, but essentially each sector begins with a unique "prologue" that is detectable when the system's trying to find a sector, and does not ever appear anywhere else. An encoding scheme is used to write out the actual data, and it takes more space than the number of bits. There is also an "epilogue" at the end of the sector. Essentially the Apple II was its own controller for the disks.
Each sector is tagged with its number so the system can scan each sector and then read in the one it wants. This tag is not part of the sector data and is written when the disk is formatted.
The later 3 1⁄2-inch drives of the mid-1980s did not use sector index
holes but instead also used synchronization patterns.
When the IBM PC came to market in 1981 (before 3.5" drives but during the 5" drive era), you could get a controller card for one or more floppy drives. This enabled the computer to ask the controller card for a track and sector, and not have to control the head and data stream directly. So programmers now dealt with a far more abstracted view. All the stuff described in chapter 3 of that Apple II reference above is something the controller does on IBM PC hardware. So programmers just had to worry about talking to the controller and not things like sector prologues, sync patterns, etc. Programming the PC floppy controller is still complex but it can do most of its work separately from the CPU.
FWIW IBM PC floppy drives also have additional data around the sector data and tag the sectors with CRC and other info.
So ... on the Apple II ....
Could you overwrite the prologue of a sector and erase the sector? Yes. The Apple DOS software would probably just hang when asked for a missing sector - it would go to the track it believes it should be and probably just read sectors forever looking for it.
Could you create your own disk routines and make really long sectors or make a whole track that's a single sector? Yes.
Many developers on the Apple II platform created custom formats to prevent copying.
Can you do this on a modern computer? On a hard drive? The answer is NO because you can only tell the controller to run commands, and some of those commands are "read sector", "write sector" but you don't have commands like "turn drive head on/off" and it probably wouldn't be possible to send commands fast enough to read/write at modern densities anyway.
Hard drives have their own integrated controllers (hence the term "IDE" for "Integrated Drive Electronics"). So low-level information like the above is only known by the device manufacturer.
You don't have control of the drive head like you do on older computers that didn't have controllers. So you are stuck with what the drive controller lets you do which is not fun actual low-level stuff.