How to Build a valid GDI from scratch
I recently purchased a GDEmu board, and one of the things I was hoping to do is experiment around with existing games by building custom GDI images. Unfortunately, after a few days messing around I've gotten stuck. There appears to be some things about a valid GD-ROM that either aren't documented, or I can't find information about them anywhere. I've gotten to the point where I can build a GDI that GD-ROM explorer believes is valid, and the DC shows the disc art on the music player, but when you try to start it you're asked to set the system clock twice then returned to the BIOS menu. Update: Solved. See below.
First of all, here's something that I haven't seen documented. Marcus Comstedt's page on IP.BIN files doesn't have anything about the TOC section. This section is just a duplicate of the track layout in the high density section. It goes from 0x100 to 0x288, and allows up to 97 tracks to be defined. (The first two tracks are part of the Low density area, thus making up the rest of the 99 track limit)
At 0x100 is the TOC header, the ASCII "TOC1". This is followed by 97 four byte entires for the remaining tracks on the disc. Each track consists of a 3 byte, little endian LBA value. This is the start LBA of the corresponding track, with 150 added to each track for some reason. So the first track in the high density area will always be 45150, and so on. The 4th byte represents the track type. 0x41 is a data track, 0x01 is an audio track. Unused tracks are filled with 0xFF.
Fairly simple, no?
So as part of my experiments, I first attempted to start modding an existing game, but when I ran into repeated problems, I decided to start over with something small and simple. The goal: rebuild the SA2 Trial demo disc from scratch and make it be bootable. It's less than 100MB and only one data track, so it seemed like a good place to start.
Steps:
1. Dump my copy of SA2: The Trial to GDI with httpd-ack. Interestingly, this disc is region free according to the IP.BIN. I didn't realize that going into this. (Does anyone know of other Dreamcast discs that were region free?)
2. Confirm the GDI boots, by testing on hardware and Makaron and NullDC emulators. [OK]
Since my intention is to use mkisofs to make the track image, which outputs raw data, first let's make sure I can do that.
3. Convert track03.bin from the game dump into raw 2048 sectors. (I used CD Image converter, to convert from 2352 Raw Mode 1 to 2048 sectors)
4. Update the .gdi file, changing the sector size for track 3 from 2532 to 2048.
5. Confirm that the original .gdi (now with 2048 byte sectors) still will boot. It does. [OK]
At this point, I know I won't need to convert my image back to 2532 sectors, (which CD Image converter can do) so that will make things a bit easier later on.
6. Extract the existing data and IP.BIN from the dump made in step 1. I saved these to a folder called data.
7. Rebuild our track03 image using mkisofs:
mkisofs.exe -C 0,45000 -V VOLUME_NAME -G IP.BIN -l -o data.iso data
We now have a new track 3, called data.iso, with the original IP.BIN as the boot sector.
8. Modify the .gdi to point to our new track 3, data.iso.
9. Pad the new ISO back to the original size by adding 00's to the end. This will allow GD-ROM Explorer to open it again. [OK]
At the initial time I posted this, I got stuck, because the resulting image would not boot. However, after watching the disc reads in a debugger, I was able to solve the final piece of the puzzle. It would always read the first sector of the TOC and stop. (It's only looking for 1ST_READ.BIN, it doesn't care about other files at this point)
10. The DC is going to read the TOC, and as soon as it finds 1ST_READ.BIN, it's going to look at the location of it. If the LBA of the file is wrong, it won't be loaded. The end of 1ST_READ.BIN must always be 150 sectors before the end of the image. (There's that 150 offset again that we saw in IP.BIN) In other words, if your 1ST_READ.BIN is 1,297,060 bytes in size like the SA2 demo, that means takes up 634 sectors. (Ceiling of 1297060 / 2048.) A complete DC disc is 549150 sectors, so 549150 - 150 - 634 = 548366. Your 1ST_READ.BIN in this case must be located at LBA 548366.
I can't really provide easy instructions for this step at the moment. In my case, I copy/pasted it there in a hex editor. Then I manually updated the location in the TOC to point at the file so it was in the same place as the original disc.
11. Test the new image. The disc will now boot.
Thus, a summary of the requirements:
1. Data must be built with the LBA starting at 45000. If the game contains CD audio, you'll need to follow steps similar to a DATA/DATA MIL-CD, where you create multiple track sessions. This is probably going to require a custom tool because it will get complicated.
2. IP.BIN must be injected to the boot sector at 45000, just like an MIL-CD would be.
3. Image must be always be a normal sized GD-ROM, due to requirement 4.
4. 1ST_READ.BIN must end exactly 150 sectors before the end of the disc.
I hope this information was useful to someone else. I haven't decided yet if I'm going to write some new tools for this process, or just take care of the 1ST_READ.BIN moving process by hand.
---
As a final note, yes, I could easily accomplish what I need to do by making a CDI image. But nobody is going to learn anything new if I do that. (And I suspect for some later titles that using GDI for modifications will avoid some nasty protection issues in the future.)
I recently purchased a GDEmu board, and one of the things I was hoping to do is experiment around with existing games by building custom GDI images. Unfortunately, after a few days messing around I've gotten stuck. There appears to be some things about a valid GD-ROM that either aren't documented, or I can't find information about them anywhere. I've gotten to the point where I can build a GDI that GD-ROM explorer believes is valid, and the DC shows the disc art on the music player, but when you try to start it you're asked to set the system clock twice then returned to the BIOS menu. Update: Solved. See below.
First of all, here's something that I haven't seen documented. Marcus Comstedt's page on IP.BIN files doesn't have anything about the TOC section. This section is just a duplicate of the track layout in the high density section. It goes from 0x100 to 0x288, and allows up to 97 tracks to be defined. (The first two tracks are part of the Low density area, thus making up the rest of the 99 track limit)
At 0x100 is the TOC header, the ASCII "TOC1". This is followed by 97 four byte entires for the remaining tracks on the disc. Each track consists of a 3 byte, little endian LBA value. This is the start LBA of the corresponding track, with 150 added to each track for some reason. So the first track in the high density area will always be 45150, and so on. The 4th byte represents the track type. 0x41 is a data track, 0x01 is an audio track. Unused tracks are filled with 0xFF.
Fairly simple, no?
So as part of my experiments, I first attempted to start modding an existing game, but when I ran into repeated problems, I decided to start over with something small and simple. The goal: rebuild the SA2 Trial demo disc from scratch and make it be bootable. It's less than 100MB and only one data track, so it seemed like a good place to start.
Steps:
1. Dump my copy of SA2: The Trial to GDI with httpd-ack. Interestingly, this disc is region free according to the IP.BIN. I didn't realize that going into this. (Does anyone know of other Dreamcast discs that were region free?)
2. Confirm the GDI boots, by testing on hardware and Makaron and NullDC emulators. [OK]
Since my intention is to use mkisofs to make the track image, which outputs raw data, first let's make sure I can do that.
3. Convert track03.bin from the game dump into raw 2048 sectors. (I used CD Image converter, to convert from 2352 Raw Mode 1 to 2048 sectors)
4. Update the .gdi file, changing the sector size for track 3 from 2532 to 2048.
5. Confirm that the original .gdi (now with 2048 byte sectors) still will boot. It does. [OK]
At this point, I know I won't need to convert my image back to 2532 sectors, (which CD Image converter can do) so that will make things a bit easier later on.
6. Extract the existing data and IP.BIN from the dump made in step 1. I saved these to a folder called data.
7. Rebuild our track03 image using mkisofs:
mkisofs.exe -C 0,45000 -V VOLUME_NAME -G IP.BIN -l -o data.iso data
We now have a new track 3, called data.iso, with the original IP.BIN as the boot sector.
8. Modify the .gdi to point to our new track 3, data.iso.
9. Pad the new ISO back to the original size by adding 00's to the end. This will allow GD-ROM Explorer to open it again. [OK]
At the initial time I posted this, I got stuck, because the resulting image would not boot. However, after watching the disc reads in a debugger, I was able to solve the final piece of the puzzle. It would always read the first sector of the TOC and stop. (It's only looking for 1ST_READ.BIN, it doesn't care about other files at this point)
10. The DC is going to read the TOC, and as soon as it finds 1ST_READ.BIN, it's going to look at the location of it. If the LBA of the file is wrong, it won't be loaded. The end of 1ST_READ.BIN must always be 150 sectors before the end of the image. (There's that 150 offset again that we saw in IP.BIN) In other words, if your 1ST_READ.BIN is 1,297,060 bytes in size like the SA2 demo, that means takes up 634 sectors. (Ceiling of 1297060 / 2048.) A complete DC disc is 549150 sectors, so 549150 - 150 - 634 = 548366. Your 1ST_READ.BIN in this case must be located at LBA 548366.
I can't really provide easy instructions for this step at the moment. In my case, I copy/pasted it there in a hex editor. Then I manually updated the location in the TOC to point at the file so it was in the same place as the original disc.
11. Test the new image. The disc will now boot.
Thus, a summary of the requirements:
1. Data must be built with the LBA starting at 45000. If the game contains CD audio, you'll need to follow steps similar to a DATA/DATA MIL-CD, where you create multiple track sessions. This is probably going to require a custom tool because it will get complicated.
2. IP.BIN must be injected to the boot sector at 45000, just like an MIL-CD would be.
3. Image must be always be a normal sized GD-ROM, due to requirement 4.
4. 1ST_READ.BIN must end exactly 150 sectors before the end of the disc.
I hope this information was useful to someone else. I haven't decided yet if I'm going to write some new tools for this process, or just take care of the 1ST_READ.BIN moving process by hand.
---
As a final note, yes, I could easily accomplish what I need to do by making a CDI image. But nobody is going to learn anything new if I do that. (And I suspect for some later titles that using GDI for modifications will avoid some nasty protection issues in the future.)