C development and patching for P01/P59

User avatar
Posts: 500
Joined: Fri Feb 02, 2018 3:13 pm

C development and patching for P01/P59

Postby NSFW » Wed Sep 30, 2020 3:14 pm

This thread was inspired by bubba2533's thoughts on using C rather than assembly for hacking on P59 firmware.

viewtopic.php?f=42&t=6247&p=102624

Programming in C is indeed easier.

The catch is that in order to develop patches in C we'll need a tool that copies the machine code from the C compiler into unused ROM, and ovewrites the target addresses of the jump instructions that you're hijacking to call your code. I wrote such a tool for Subaru hacking years ago that could probably work for P01/P59 if we have a 68332 compiler that produces Motorola S-record output. It's not the most elegant approach but it was pretty easy to implement and it has some features that I like.

The code:
https://github.com/LegacyNsfw/RomPatch

Instructions for end users:
http://www.romraider.com/forum/viewtopi ... =32&t=7892

Things I like about this design:

- The source code contains the data that the patch utility needs.

- So, instead of copying an address from the compiler output into a metadata file, you can just put the function name into the metadata file and the tools take care of the rest.

- The patch (assembly code) and the metadata (which data to put where) get built into a single file, so you don't have to maintain the patch metadata separately from a patch bin file.

This is what I mean by metadata:
https://github.com/LegacyNsfw/EcuHacks/ ... Metadata.s

As a developer, the workflow looks like:
1. compile and link the C code with some extra options that tell them to generate an Srecord file, and how to handle the metadata
2. run the patch tool with an option that tells it to copy the patch and metadata into a new Srecord file.
3. Done.

End users can then run the patch tool with their bin file and an Srecord file, and it will take care of the rest.

To make this work for GM PCMs, we'll need a compiler that can produce 68332 / CPU32 code, and some experimenting to get it to produce the Srecord output that the path tool needs.

This is the C project that contains my Subaru stuff:
https://github.com/LegacyNsfw/EcuHacks

And this is a related thread about the development tools I was using for Subaru stuff. I wish I knew of a GUI debugger like this for 68332 development, it made Subaru hacking easier that I ever imagined. Or maybe "easier" is the wrong word but it was much less hard than I imagined. :) But anyway, it has some context about the patch tool and the C project:
http://www.romraider.com/forum/viewtopi ... =40&t=7680
Please don't PM me with technical questions - start a thread instead, and send me a link to it. That way I can answer in public, and help other people who have the same question. Thanks!

Site Admin
User avatar
Posts: 6562
Joined: Sat Feb 28, 2009 8:34 pm

Re: C development and patching for P01/P59

Postby antus » Wed Sep 30, 2020 4:32 pm

I think we should use the same toolset as we use for the flash kernels, with a similar/same build system. objcopy will convert to srecord, or we could update the tooling to use bins.

It might be possible to emulate in easy68k but I think it would mean compiling c to asm, and getting that asm in to easy68k (not sure if compatible? or if it can run executable payloads straight up)? http://www.easy68k.com/QuickStart/LoadPageFrames.htm
The site index is down, but additionally the code is on github. https://github.com/ProfKelly/EASy68K

It wouldnt be able emulate the whole OS but you could essentially unit test a function or a couple of functions.
Have you read the FAQ? For lots of information and links to significant threads see here: viewtopic.php?f=7&t=1396

User avatar
Posts: 500
Joined: Fri Feb 02, 2018 3:13 pm

Re: C development and patching for P01/P59

Postby NSFW » Thu Oct 01, 2020 6:00 pm

Oh right, we have a compiler. :)

Parts of the easy68k site still work:
http://www.easy68k.com/features.htm

The binaries are on github:
https://github.com/ProfKelly/EASy68K

It says the emulator supports srecord files, so I'm guessing it will be possible to load and step through srecord files generated by the compiler.
Please don't PM me with technical questions - start a thread instead, and send me a link to it. That way I can answer in public, and help other people who have the same question. Thanks!

User avatar
Posts: 1087
Joined: Sat Dec 15, 2018 7:38 am

Re: C development and patching for P01/P59

Postby Gampy » Thu Oct 01, 2020 7:00 pm

I may be mistaken for I am no expert, however I do believe the GNU m68k toolchain used for the kernels is incompatible with Easy68k ...

Yes, the GNU m68k toolchain does output S-Records.

[edit]
After further investigation, it appears GNU m68k Style is configurable, maybe there is a compatible style.
Improve Knowledge -=- Share Yours!

Posts: 110
Joined: Wed Apr 11, 2018 8:50 am

Re: C development and patching for P01/P59

Postby bubba2533 » Wed Mar 31, 2021 1:26 pm

I would like to help with this but I don't think I know enough to really help.

I will force myself to learn C once this us up and running. Writing a simple spark launch control in ASM was terrible and took forever.

I'd like to program a PID launch control routine, but don't even want to think about doing it without a higher level language.

User avatar
Posts: 1087
Joined: Sat Dec 15, 2018 7:38 am

Re: C development and patching for P01/P59

Postby Gampy » Wed Mar 31, 2021 11:31 pm

What we need to do is get m68k added to Compiler Explorer, that would make it oh so easy to code patches.
Improve Knowledge -=- Share Yours!

Return to Tool Development

Who is online

Users browsing this forum: No registered users and 1 guest