User:Combuster/Dreamcast Barebones

From OSDev Wiki
Jump to: navigation, search
Difficulty level
Difficulty 2.png
Medium

Among the most common household Hitachi SuperH platform is the Sega Dreamcast. Since getting a working setup for this platform is non-trivial due to the amount of protection on the device. This tutorial is an A-Z on getting your first code to work on the dreamcast.

Contents

The Toolchain

There are several tools you will need. First and foremost is the compiler. Since you are most likely developing on an PC, you'll need a dedicated tool for targeting a different architecture. Basically the steps are nearly identical to GCC Cross-Compiler, but with a different target. Grab a copy of Cygwin, The GCC and binutils sources, and make sure Cygwin has the necessary tools installed. A short version of the instructions follow which should be sufficient if you built a cross-compiler before. If you haven't done so before, please read the cross-compiler tutorial for an in-depth

export TARGET=sh4-elf
export PREFIX=/usr/cross

We set up two variables: TARGET holds the architecture we will be targeting: ELF on SH4 (the instance of the processor found in a dreamcast), the prefix is for putting the cross-compiler somewhere safe from messing up the local system

cd /usr/src
mkdir build-binutils-2
cd build-binutils-2
../binutils-x.xx/configure --target=$TARGET --prefix=$PREFIX --disable-nls
make all install

The path is different here so that you don't get conflicts between your previous Binutils/GCC build and this one.

export PATH=$PATH:/usr/cross/bin
cd /usr/src
mkdir build-gcc-2
cd build-gcc-2
../gcc-x.x.x/configure --target=$TARGET --prefix=$PREFIX --disable-nls --enable-languages=c --without-headers
make all-gcc install-gcc

Furthermore, you need mkisofs to generate a CD filesystem, and two dedicated tools that deal with the copy protection: cdi4dc (windows and linux binaries only) and [mc.pp.se/dc/files/scramble.c scramble]. This process will create a CDI format CD image, this is because the standard ISOs do not store all the data on the CD, but only the 2048 bytes of each logical data sector.

Building the kernel

The SH4 is a bi-endian architecture, and can run in either little-endian or big-endian mode. Since the bootloader starts in little-endian mode, it is easiest to use the same endianness in your own code. Since the assembler defaults to big-endian, we need to override it with each invocation:

sh4-elf-gcc -ml -c -o file.o file.c
sh4-elf-as -little -o file.o file.s

The kernel is expected to be a flat binary and gets loaded at 0x8c010000, which requires us to use the following linker script:

OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x8c010000;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    *(.rodata)
    . = ALIGN(4096);
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}

and you can link your kernel as follows:

sh4-elf-ld -o kernel.bin -T link.ld -lgcc -L/usr/cross/lib/gcc/sh4-elf/x.x.x/

Building the image

The dreamcast uses a form of encryption to load a binary. For the kernel to work we need to encrypt it, so it will come out correctly when decrypted. We also give the kernel binary the required name here:

./scramble kernel.bin 1ST_READ.BIN

Now continue building the image

mkdir cdrom
cp 1ST_READ.BIN cdrom
mkisofs -G IP.BIN -C 0,11702 -J -l -r -V "MyImage" -o temp.iso cdrom
cdi4dc temp.iso output.cdi

The process starts with setting up a subtree for the CD contents, we put the kernel in there, as well as any other files we want to put on the CD. This folder is then used as the root of the CD. The remaining arguments are as follows:

  • -G IP.BIN
this selects what binary to put at the bootable section of the image. We point it to SEGA's binary blob as its contents is verified by the BIOS. Note that this piece is also responsible for displaying the "produced by or under license from" message, designed to sue malicious parties over trademark infringement.
  • -C 0,11702
This is part of the DC protection scheme. While the intricate details are handled by cdi4dc, we have to tell mkisofs that the filesystem will not be put at the start of the disc, but several sectors later (11702 to be precise). mkisofs will add this offset to all references to keep the filesystem working on both the dreamcast and PC.
  • -J
Enable Joliet extensions which allows long filenames to be stored
  • -l
Disable DOS-style 8.3 formatting
  • -r
Remove permissions to prevent problems with owners and access rights.
  • -V "MyImage"
Set the volume name. Mainly for aesthetic purposes, modify as necessary.
  • -o temp.iso
Output file, since its unusable as ISO image (thanks to -C), its a temporary product
  • cdrom
Root of the filesystem, in our case its the cdrom directory

See Also

External links

Personal tools
Namespaces
Variants
Actions
Navigation
About
Toolbox