News: 11 March 2016 - Forum Rules
Current Moderators - DarkSol, KingMike, MathOnNapkins, Azkadellia, Danke

Author Topic: [PS1] Dealing with Mode2/Form2 data files (Solved)  (Read 2496 times)

acediez

  • Hero Member
  • *****
  • Posts: 502
    • View Profile
[PS1] Dealing with Mode2/Form2 data files (Solved)
« on: August 19, 2020, 08:35:59 pm »
I'm working on a PS1 game with a Mode2/Form2 data file (Goemon Shin Sedai Shuumei).
On one hand, I'm able to extract the RAW file completely (2352 bytes per sectors) using XtrActor.NET, and I've managed to rebuild the disc with all the original files using mkpsxiso (defining the file as a regular "XA" filetype). Now I need to be able to do the same with a completely modified version of this Mode2/Form2 file.

I have no problems dealing with the file itself since I can extract a "clean" data file using IsoBuster (just the 2324 bytes of data per sector). But bringing it back to a Mode2/Form2 after modifying it, with the correct header data, will be a challenge...

I'm starting to get familiar with the contents of the header by checking the existing documentation...
Code: [Select]
Mode2/Form2 (CD-XA)
  000h 0Ch  Sync
  00Ch 4    Header (Minute,Second,Sector,Mode=02h)
  010h 4    Sub-Header (File, Channel, Submode OR 20h, Codinginfo)
  014h 4    Copy of Sub-Header
  018h 914h Data (2324 bytes)
  92Ch 4    EDC (checksum accross [010h..92Bh]) (or 00000000h if no EDC)

... but I was wondering, is there are any existing tools to split the file in sectors and "calculate" and fill the header/subheader data?
Or will I need to figure it out completely and write my own tools?

Thanks in advance for any leads
« Last Edit: September 03, 2020, 03:34:47 pm by acediez »

tvtoon

  • Sr. Member
  • ****
  • Posts: 371
    • View Profile
Re: [PS1] Dealing with Mode2/Form2 data files
« Reply #1 on: August 22, 2020, 04:14:16 pm »
I am not sure on what you want, if you want to calculate the file size in sectors, it is just math: file size divided by RAW sector size (2352). When using tracks, you need to have the table of contents on hand, as almost everything is data.

Now, if you are looking for something to build the XA header for you because you are changing them, that is a whole different story... You need to take care of how the game handles the flags, usually it looks simple enough until you play that on real hardware.
 :thumbsup:

acediez

  • Hero Member
  • *****
  • Posts: 502
    • View Profile
Re: [PS1] Dealing with Mode2/Form2 data files (Solved)
« Reply #2 on: August 28, 2020, 08:10:32 pm »
Thanks for the heads up!

I'm gonna mark this as "solved" for now, as I figured out what I set out to do with the initial post. I'll further explain what it was in case it's helpful for other people working on PS1 games:

The game I'm working on has most of the game assets within a single packed file, which is stored in Mode2/Form2 in the disc. I've (mostly) already figured out the file itself. By debugging in-game, I found a table of contents (offsets and filesizes) within the game's executable, and was been able to unpack it by extracting the "clean" data using IsoBuster (just the 2324 bytes of data on each sector). My question in the initial post was about adding back the sector data into this cleaned up file, so I can put it back in the disc again after any changes. As a first learning step, I set out to simply convert the extracted unheadered file back to a proper 2336 bytes/sector file.

After I wrote this thread, I spent some time reading about sector data. I ran across this very helpful document:

Disc Format Standard Specifications Ver. 1.0 (ST-040-R4-051795)
https://segaretro.org/images/b/be/ST-040-R4-051795.pdf

Before reading through this, I had the misconception that I had to write the block address (main header) myself (that's what I meant by "calculate" in my first post), but that's actually not the part of the sector data I need to fill manually:

Code: [Select]
RAW file (2352 bytes per sector) (This is what I see on the final disc image)
0Ch   Sync
4h    Header (Minute,Second,Sector,Mode=02h)
4h    Sub-Header (File, Channel, Submode OR 20h, Codinginfo)
4h    Copy of Sub-Header
914h  Data (2324 bytes)
4h    EDC (checksum accross [010h..92Bh]) (or 00000000h if no EDC)

Unheadered file (2336 bytes per sector) (This is what I get by extracting the file using XtrActor.NET, and what I need to build the disc from scratch)
4h    Sub-Header (File, Channel, Submode OR 20h, Codinginfo)
4h    Copy of Sub-Header
914h  Data (2324 bytes)
4h    EDC (checksum accross [010h..92Bh]) (or 00000000h if no EDC)

"Clean" data-only file (2324 bytes per sector) (This is what I need to actually be able read the files and modify them)
914h  Data (2324 bytes)

So, it's a lot simpler than I thought. The "sync" field is always the same, the "header", as tvtoon said, is just the position of the file within the disc. Both of these fields are taken care of mkpsxiso, as well as the "EDC" data, which is generated when creating the disc. All I really need to fill beforehand is the "subheader" and padd out the "EDC" bytes with zeros.

Being a single file, the "subheader" is fortunately the same value all the way through, except in the last sector, which adds a bit that marks the end of the file (pages 12-16 from the document explain exactly what each bit on the subheader mean).

Knowing this, I was able to write a quick script that fills the data as needed. The resulting file is identical to the original, so it works in-game. Now I'm ready to go for the next step, which is to recreate the process with an actual modified repacked file.

Stay tuned for my next roadblock.

niuus

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Re: [PS1] Dealing with Mode2/Form2 data files (Solved)
« Reply #3 on: August 29, 2020, 12:45:27 am »
I'm working on a PS1 game with a Mode2/Form2 data file (Goemon Shin Sedai Shuumei).
On one hand, I'm able to extract the RAW file completely (2352 bytes per sectors) using XtrActor.NET, and I've managed to rebuild the disc with all the original files using mkpsxiso (defining the file as a regular "XA" filetype). Now I need to be able to do the same with a completely modified version of this Mode2/Form2 file.
Off topic, just wanted to say thanks since i was looking for a modern solution to completely extract a PSX game, replace all the FMVs with a dummy video file, and then be able to rebuild a valid ISO to use on the console or an emulator. Also stumbled with your other thread here. Now i had a clue where to start.   :beer:

I generated the xml, added the image_name attribute, rebuild with mkpsxiso, and finally psxlicense to the generated bin, but i could not get FF IX to boot again on the emulator. Maybe i am missing something in between?
« Last Edit: August 29, 2020, 03:40:09 am by niuus »

MysticLord

  • Full Member
  • ***
  • Posts: 160
    • View Profile
Re: [PS1] Dealing with Mode2/Form2 data files (Solved)
« Reply #4 on: August 29, 2020, 04:11:15 am »
edit

whoops, wrong thread
« Last Edit: August 29, 2020, 03:37:27 pm by MysticLord »

acediez

  • Hero Member
  • *****
  • Posts: 502
    • View Profile
Re: [PS1] Dealing with Mode2/Form2 data files (Solved)
« Reply #5 on: August 29, 2020, 11:00:05 am »
Off topic, just wanted to say thanks since i was looking for a modern solution to completely extract a PSX game, replace all the FMVs with a dummy video file, and then be able to rebuild a valid ISO to use on the console or an emulator. Also stumbled with your other thread here. Now i had a clue where to start.   :beer:

Great! The tools I described are the ones I've been using for all the PS1 projects where I've changed more than one file, including STR videos (Mega Man 8, X4 and X5): extract Mode1 files with CDmage, extract Mode2 files XtrActor.NET, get license files from PSYQ_SDK, and build the disc with mkpsxiso.

However, depending on the game, there's likely more work involved than simply dropping the files into a disc. Some (most?) games will require you to write files in the same order. For this, you can use IsoBuster and sort files by "LBA", which is the disc sector where each file is located. That'll give you the order in which you have to list the files in the XML you write for mkpsxiso.



However if the game is looking for files in an specific location, it's also possible that it has its own table of contents of the disc somewhere in the code, besides the standard table of contents built in the disc format. Most likely, inside the main executable. This was the case for Mega Man 8 and X4, and even being games from the same company and within a short development time frame, their tables of contents have a different format. These can vary wildly for each developer/game. To find it and being able to update it with different LBA/filesizes, you'll have to either debug the game and find it whenever it's being read, or ask help of people who have already worked on that specific game. So if you're working on Final Fantasy game, I add to the previous reply, forums.qhimm.com might be a good place to ask.

In fact, since it's a game that's already been worked on heavily, there might be knowledge of a different approach to do what you mean to do. If all you want to do is skip FMVs, you could achieve that by removing from code the subroutines that start each video (for example), no disc rebuilding involved.
« Last Edit: August 31, 2020, 04:03:58 pm by acediez »