The following information was kindly contributed by our forum member Lydux.
Original thread can be found here: http://nfggames.com/forum2/index.php?topic=5417.0
So I've decided to share with you some of my study and work. Here is small zip archive containing some part of my DDK and (unfinished) samples : http://nfggames.com/x68000/Development/libraries/human68k-ddk-0.1.zip
It does not contain everything I've written ! (My hard drive is a mess of source codes… :p ) Also, it's more linux friendly than Code::Blocks, but I remember having compiled the WINDRV sample on Code::Blocks. At least, files are correctly commented and you should find the necessary to build a simple driver. So, consider this as a reference actually. I'll try to create a repository on my github page soon.
Note : the following contains very hard stuff ! I might be confusing sometimes, sorry in advance. Don't hesitate to ask for help if you do not understand something. Thank you.
Terminology :
Normally, any traditional executable requires some initializations step before reaching the “main” subroutine such as the process memory allocator, environment variables, command line processing, etc… This code object (crt0.o) is transparent for you, is provided by the libc (newlib) and automatically linked with the whole final objects by the toolchain linker specs. However, such initialization step is meaningless for driver ! Well… At least for .SYS drivers. You might have noticed that drivers may also be .X files ? In fact they contain both driver AND executable code.
So depending on your wish :
When Human68k load drivers by CONFIG.SYS, the kernel hasn't totally finished initializing itself. Some libc functions will not be available. For example, COMMAND.X is not loaded yet, so you'll lack “setenv” and “getenv” functions. Also, you can consider a driver as a part of the kernel ; that means the overall RAM consummed by the kernel is not yet known ! The libc memory allocator can't be used, so no malloc available !
Because many of such functions can't be used with driver, I recommand not to use my toolchain libc by adding “-nostdlib” to the linker flags.
The zip archive contains some stripped down version of usefull libc's functions to help you instead. (look at the include folder).
That's all about startup and libc.
About libraries, you'll probably find interest in iocscalls and doscalls, they remain fully valid with drivers. You can safelly add “-liocs -ldos” to library link.
First, please note that Human68k drivers really look like to MS-DOS ones. So chances are MS-DOS drivers documentations are also valid to Human68K !
When kernel try to load a driver, the very first thing it must encounter is an header containing informations on how to load it and which kind of driver it is. The very first driver header must be located right after the XFile header which is 0x40 bytes length. Here is this header structure :
These 2 routines are the only ones used by the kernel to communicate with the driver.
Upon header validation, the kernel will allocate a few bytes in its own memory that'll be used later as a communication interface. This small memory block is fixed in size, will never move, and will remain forever during the OS lifetime. We call it the “Request Packet”. Once allocated, it's absolute location is set into cpu register A5 and will imediatly jump to the “Strategy routine”. On Human68k, the only purpose of this routine is to store the value contained in the register A5 somewhere in the driver uninitialized data space (the complex and long word to say a “variable” in C. :P Or BSS segment in binary terminology.) and return. Then, the strategy routine can totally be forgotten, it will never be reused.
Next is the “Interrupt routine”, and is the most important one. All kernel request to driver will be passed by modifying the request packet, and then execute the interrupt routine.
This routine is responsible of analyzing the request packet, and proceed the request.
The small allocated kernel memory area will change on every kernel request. Only the first 13 bytes is a fixed structure describing the request itself, and the remaining are the request parameters. Note that the some Request Packet structure members are filled by the kernel (input), others are by the drivers as the result of the request (output).
Request packet structure header (fixed first 13 bytes) :
Command execution result flags :
This list contains all acceptable values to fill the result status flags in the request packet header. S_ flags will display (or hide) the “A”bort, “R”etry and “I”gnore line, and E_ will show an appropriate error message.
“Result status” member can be any combination of S_ flags (ORed together), but only one E_ can be set.
Each possible request command code in the request packet header have their own set of parameters. Again some are filled by the kernel, and others by the driver.
Note that some request command code are available only for character device drivers, some only for block device drivers and some on both. Remote device drivers have their own set of command code. Command parameters structure always start at position 14 within the request packet.
Command ID : 0x00 (C_INIT)
Availability : Block/Character
Description :
This is the initialize command and is called only once after driver loading. On initialize, the driver must perform eventual device detection and initialization, specify the number of unit it will handle, process its own arguments passed by CONFIG.SYS, and specify its eventual memory heap size. On return from this command, this finish the whole driver initialization process, and the kernel will continue to the next driver or continue its execution.
Input parameters :
Position | Type | Description |
---|---|---|
18 | Long | Pointer to a null terminated string containing arguments passed via CONFIG.SYS |
22 | Byte | Block device driver only. Not sure of the meaning, but this probably contains the drive number the kernel want to initialize. |
Output parameters :
Position | Type | Description |
---|---|---|
13 | Byte | Block device driver only. The amount of “units” this driver will handle. Ex : amount of partitions in all detected hard drives |
14 | Long | Pointer to the end of the used memory space. This memory space include every segment sizes used by the driver : TEXT, DATA, BSS, as well as eventual HEAP. A very important parameter ! Any wrong value written here will crash the kernel. Normally, my toolchain provides a symbol usable for this parameter : “_end”. “_end” points to the last byte of the relocated BSS segment, so you can directly pass its address if you don't use HEAP, or pass “_end” address + the amount of memory you need for HEAP. See DDK samples for more infos. |
18 | Long | Block device driver only. Pointer to an array of BPB (Bios Parameter Block). Basically, a BPB describes the FAT volume on the physical disk, so determine the size of a partition or media. For more information about BPB, see wikipedia : http://en.wikipedia.org/wiki/BIOS_parameter_block Note that the X68000 BPB is slightly different than PC BPB. I will describe it later. |
Command ID : 0x01 (C_MEDIACHK)
Availability : Block
Description : Before performing IO operations on a block device, the kernel will process this command code to ensure the target media is still available and ready.
Input parameters :
Position | Type | Description |
---|---|---|
13 | Byte | Last known media type ID. See standard media type list bellow. |
Output parameters :
Position | Type | Description |
---|---|---|
14 | Byte | Media status. Possible values are : |
-1 | M_CHANGED : Media was changed | |
0 | M_DONT_KNOW : Media state unknown | |
1 | M_NOT_CHANGED : Media still in and ready |
Human68K standard media type list :
Note : this list isn't exhaustive ! A driver can provide its own set. So, a program or driver should never rely on these values to determine the media type.
I will not explain them, names pretty much speak for themselves.
Command ID : 0x02 (C_BLDBPB)
Availability : Block
Description : This command is proceed when a media have previously been reported as “changed” by the “Media Check” command code. It's used to fetch the newer unit BPB.
Input parameters : none
Output parameters :
Position | Type | Description |
---|---|---|
18 | Long | Pointer to the requested unit BPB |
Command ID : 0x03 (C_IOCTLIN)
Availability : Block/Character
Description : Request the driver to read raw arbitrary datas from the specified unit. This request is initiated by an userspace application (IOCTL).
Input parameters :
Position | Type | Description |
---|---|---|
14 | Long | Pointer to a buffer that'll contain readed data from the unit. The driver is responsible of filling this buffer. |
18 | Long | Buffer size |
Command ID : 0x04 (C_INPUT)
Availability : Block/Character
Description : Request the driver to read data from the specified unit. This request is initiated by the kernel.
Input parameters :
Position | Type | Description |
---|---|---|
13 | Byte | Block device driver only. Media type. |
14 | Long | Pointer to a buffer that'll contain the readed data from the unit.The driver is responsible of filling this buffer. |
18 | Long | Block device driver : number of sector to read. A sector length is normally specified by the BPB. |
Character device driver : Buffer size. | ||
22 | Long | Block device driver only : logical start sector to read from. |
Output parameters : none
Note for block device driver : as the kernel is responsible for handling the FAT, you just have to seek to the specified start sector location and read the wanted amount of data from there according to the requested amount of sectors and the BPB. However, the driver is still responsible of the logical to physical partition translation ! So, seek correctly.
Command ID : 0x05 (C_NDREAD)
Availability : Character
Description : To check furthermore. A strange command code that allow a driver to send back a device prereaded byte. Maybe dedicated command to some simple interrupt driven device.
Input parameters : none
Output parameters :
Position | Type | Description |
---|---|---|
13 | Byte | Prereaded byte |
Command ID : 0x05 (C_DRVCTL)
Availability : Block
Description : To check furthermore. This seems to be really X68000 specific floppy drive command, or some kind of kernel hack by Human68k developpers. It does some kind of IOCTL, but internally to the kernel to dis/allow and control of FDD ejection, LEDs or media write protection control…
Command ID : 0x06 (C_ISTAT)
Availability : Character
Description : Kernel perform this command before the INPUT one to check if reading on the specified unit is actually possible or not. There is no parameter. Just fill the result flags in the request packet header accordingly.
Input parameters : none
Output parameters : none
Command ID : 0x07 (C_IFLUSH)
Availability : Character
Description : Request the driver to flush the specified unit input buffer. There is no parameter. Just fill the result flags in the request packet header accordingly.
Input parameters : none
Output parameters : none
Command ID : 0x08 (C_OUTPUT)
Availability : Block/Character
Description : Request the driver to write data to a specified unit. This request is initiated by the kernel.
Input parameters :
Position | Type | Description |
---|---|---|
14 | Long | Pointer to a buffer that contains data to write to the unit. |
18 | Long | Block device driver : number of sector to write. A sector length is normally specified by the BPB. |
Character device driver : Buffer size. | ||
22 | Long | Block device driver only : logical start sector to write to. |
Output parameters : none
Note for block device driver : as the kernel is responsible for handling the FAT, you just have to seek to the specified start sector location and read the wanted amount of data from there according to the requested amount of sectors and the BPB. However, the driver is still responsible of the logical to physical partition translation ! So, seek correctly.
Command ID : 0x09 (C_OUTVFY)
Availability : Block/Character
Description : Same as “0x08 - Output (write)”, but the driver must also read the data buffer back to ensure the data as been correctly written.
Input parameters : same as the “0x08 - Output (write)”
Command ID : 0x0A (C_OSTAT)
Availability : Character
Description : Kernel perform this command before the OUTPUT one to check if writing to the specified unit is actually possible or not. There is no parameter. Just fill the result flags in the request packet header accordingly.
Input parameters : none
Output parameters : none
Command ID : 0x0B (C_OFLUSH)
Availability : Character
Description : Request the driver to flush the specified unit output buffer. There is no parameter. Just fill the result flags in the request packet header accordingly.
Input parameters : none
Output parameters : none
Command ID : 0x0C (C_IOCTLOUT)
Availability : Block/Character
Description : Request the driver to write raw arbitrary datas to the specified unit. This request is initiated by an userspace application (IOCTL).
Input parameters :
Position | Type | Description |
---|---|---|
14 | Long | Pointer to a buffer that'll contain data to write to the unit. The userspace program is responsible of filling this buffer. |
18 | Long | Buffer size |
Output parameters : none
Command ID : 0x13 (C_GENIOCTL)
Availability : Block/Character
Description : Perform a IOCTL command understood only by the driver and an userspace program.
Input parameters :
Position | Type | Description |
---|---|---|
14 | Long | Optional pointer to a buffer. That buffer is totally specific to the IOCTL command ID. |
18 | Word | IOCTL command ID. There is no restriction to this value. |
Output parameters : none
These following request command codes are only performed when the driver header have the “attribute” flag set with the value 0x2000 (Remote device driver).
I actually do not know very much about them but they seems to be really filesystem level.
Here is all known command ID :
I do have parameters. I will write them later.
I will not explain this structure as there is plenty of informations about it on the internet.
Starting from wikipedia : http://en.wikipedia.org/wiki/BIOS_parameter_block
Just read everything you can about FAT12 and FAT16. 95% of these also apply to Human68k.
Most of difference is the byte ordering, which is big endian instead of little endian. The structure is also a bit different.
From my ddk.h file :
struct bpb { UWORD bpb_nbyte; /* Bytes per sector */ UBYTE bpb_nsector; /* Sectors per allocation unit */ UBYTE bpb_nfat; /* Number of FATs */ UWORD bpb_nreserved; /* Number of reserved sectors */ UWORD bpb_ndirent; /* Number of root directory entries */ UWORD bpb_nsize; /* Size in sectors (16 bits) */ UBYTE bpb_mdesc; /* MEDIA Descriptor Byte */ UBYTE bpb_nfsect; /* Number of sector per FAT */ ULONG bpb_huge; /* Size in sectors if bpb_nsize == 0 (32 bits) */ };
See ? Only the “number of FAT” and “number of reserved sectors” members are swapped.