Modify E40 OS Routine - CAN

Disassembly, Reassembly, Tools and devleopment. Going deep with Hardware and Software.
User avatar
Tre-Cool
Posts: 544
Joined: Tue Oct 16, 2012 2:17 am
cars: VY SS UTE, VX Drag Car
Location: Perth

Modify E40 OS Routine - CAN

Post by Tre-Cool »

So before I have to resort to a hardware solution & due to the fact I'm not a coder by any means. I'm reaching out to see if anyone has any skills in Motorola code or being able to help assist me modify the os routines in ghidra?

What i require is putting data into an existing can broadcast for $1A1. I have found the routines for byte 0 & byte 1, but i need to have data sent out on byte 2 for the park neutral switch status & Validity.
At the raw can level, sending 00 00 41 seems to be what i need to send when in gear & 00 00 01 when in park/neutral. Ignore the first 2 byte blocks I'm just using 0's as example, the ecu does in fact update those 2 first blocks.

Byte 0 C-code Breakdown
1A1-Byte0-1.png
1A1-Byte0-2.png
Byte 1 C-code Breakdown
1A1-Byte1-1.png
1A1-Byte1-2.png
1A1 Function C-Code
1A1-Build.png
What I'm hoping we can do is just edit the existing 1A1 function to include the 2nd byte count by utilizing an existing switch in ram to set the above PN state.
VbTRGI_TransGearEngagState

Remember I have no coding experience, but I'm assuming it would be something like this eventually.
1A1 Function C-Code
uVar1 = BYTE_BuildENGN_y_1A1_Byte0();
*extraout_A1 = uVar1;
uVar1 = BYTE_BuildENGN_y_1A1_Byte1();
*(undefined1 *)(extraout_A1_00 + 1) = uVar1;
uVar1 = BYTE_BuildENGN_y_1A1_Byte2();
*(undefined1 *)(extraout_A1_00 + 2) = Uvar1;
*(undefined1 *)(extraout_A1_00 + 3) = 0;
*(undefined1 *)(extraout_A1_00 + 4) = 0;
*(undefined1 *)(extraout_A1_00 + 5) = 0;
*(undefined1 *)(extraout_A1_00 + 6) = 0;
*(undefined1 *)(extraout_A1_00 + 7) = 0;
return;
Byte 2 - C-code
uint BYTE_BuildENGN_y_1A1_Byte2(void)

{
uint uVar1;

if (_VbTRGI_TransGearEngagState != 1) {
uVar1 = uVar1 | x40;
}
else {
uVar1 = 0;
}
return uVar1;
Or Just:
uVar1 = BYTE_BuildENGN_y_1A1_Byte0();
*extraout_A1 = uVar1;
uVar1 = BYTE_BuildENGN_y_1A1_Byte1();
*(undefined1 *)(extraout_A1_00 + 1) = uVar1;
*(undefined1 *)(extraout_A1_00 + 2) = 41;
*(undefined1 *)(extraout_A1_00 + 3) = 0;
*(undefined1 *)(extraout_A1_00 + 4) = 0;
*(undefined1 *)(extraout_A1_00 + 5) = 0;
*(undefined1 *)(extraout_A1_00 + 6) = 0;
*(undefined1 *)(extraout_A1_00 + 7) = 0;
return;
This method just transmits fixed data?
You do not have the required permissions to view the files attached to this post.
Last edited by Tre-Cool on Wed Nov 12, 2025 4:11 am, edited 1 time in total.
kur4o
Posts: 1154
Joined: Sun Apr 10, 2016 11:20 am

Re: Modify E40 OS Routine - CAN

Post by kur4o »

Small patches like that are easier to be handled in Assembly language.

where byte 2 is cleared you can make a jump to some unused space in OS area. read status of p/n switch[not sure it is bitencoded or boolean]

copy result to d0,
when return to function add
move d0 to byte2- a1 register.

keep track of registers being used, stack any if needed.

than make sure rest of of unused bytes are cleared too.

It is possible to have some switches how long each CANID broadcast is. So you need to figure that too, if it still sends 2 bytes message.

Also make sure all code starts at even number. If it starts at odd number expect crashes.
User avatar
antus
Site Admin
Posts: 10111
Joined: Sat Feb 28, 2009 10:34 am
cars: TX Gemini 2L Twincam 8psi
TX Gemini SR20 18psi
Datsun 1200 Ute
Subaru Blitzen '06 EZ30 4th gen, 3.0R Spec B
Subaru WRX 2007

Re: Modify E40 OS Routine - CAN

Post by antus »

I see kur40 got in first, but I'll still post this :)

Firstly, to patch, you'll find it a lot easier to work in assembly code. To do it in C means you'll need a compiler and it'll create larger code that is harder to work with and get in the OS.
The final suggestion looks closest. You can see in the disassembly

13 40 00 01 move.b D0.b, (0x1, A1)
42 29 00 02 clr.b (0x2, A1)

What you have hear is address register A1 holds the address of the reply buffer. the (0x1, A1) and (0x2, A1) syntax represents an offset from that address. So the first command means 1 byte after the start of the buffer, the second means 2 bytes after the stat of the buffer.
The move.b is saying move a byte (thats .b) in to that location, and that byte is in data register D0, lowest byte (again .b).
The clr.b is saying to zero out the 2nd byte of the reply.

Noteworthy both commands are 4 bytes, you can patch over 42 29 00 02 with 13 40 00 02 to copy D0.b in to the second byte and it'll fit. That gets something in there. But you want to put your own value in there.

So lets see if we can come up with a 4 byte opcode that includes the byte to go in there. If we can then you can hard code it over the top.

So I checked the pcmhammer flash kernel because I am familiar with it, and I know I am doing this to copy the version byte in to an offset in the reply for the kernel version response. So I loaded the assembled kernel in ida and see the function has become:

117C 000A 0008 move.b #$A,8(a0)

The syntax is slightly different as is GNU not Motorola but you can still read it, and you can see how the data bytes end up in the opcodes. So its writing 'A' (value 10, this is the P10 kernel and its communicating that as part of the version) in to byte offset 8 deep from Address register 0.

So what you'd want, if your value was 1 and your offset was 2 would be 11 7C 00 01 00 02. Problem is, that is now 6 bytes and you only have 4 to play with.

To solve that you'll need to remove something, or jump to some unused space, run this opcode, then jump back. Which is little slower to run. Next you need to look at jump opcodes, or see if you can come up with a way to achieve this in 4 bytes.

Looking at it, I also notice the clr commands are all in a row, and 4 bytes each. You might be able to change some of them to write 2 bytes at once, and get a couple of bytes back by optimising that code. Something like clr.w (0x4, A0) might clean up 2 bytes in one opcode, which might be 6 or 8 long.... If its too short you can put a nop (no operation) opcode in as filler.

Making sense?
Have you read the FAQ? For lots of information and links to significant threads see here: http://pcmhacking.net/forums/viewtopic.php?f=7&t=1396
User avatar
Tazzi
Posts: 3627
Joined: Thu May 17, 2012 10:53 am
cars: VE SS Ute
Location: WA

Re: Modify E40 OS Routine - CAN

Post by Tazzi »

Alright.. so.. when I patch code... all I care about is being in the correct function, I do not care about how many opcodes I have to overwrite, only that I will need to copy them across to the new function created that gets jumped to.

For example, using the two byte ASM example that Antus mentioned:

Code: Select all

13 40 00 01 move.b D0.b, (0x1, A1)
42 29 00 02 clr.b (0x2, A1)
I would replaced those two with the following (Makes up a perfect 8 bytes to replace the 8 above):

Code: Select all

4EF9 11223344 - this is JMP to address 11223344
4E71 - NOP (assume this exact opcode location is 0x00A0B0C0)
At the new jump address, I will then add:

Code: Select all

13 40 00 01 move.b D0.b, (0x1, A1)
42 29 00 02 clr.b (0x2, A1)
.. save registers to stack here..
.. perform custom ASM operations here..
.. restore registers from stack..
4EF9 00A0B0C0 - JMP back to address 0x00A0B0C0 which is where we placed our nop in the original function.
A great place to learn and understand ASM commands is:
https://mrjester.hapisan.com/04_MC68/Index.html
https://www.nxp.com/docs/en/reference-m ... 000PRM.pdf
Your Local Aussie Reverse Engineer
Contact for Software/Hardware development and Reverse Engineering
Site:https://www.envyouscustoms.com
Mob:+61406 140 726
Image
User avatar
antus
Site Admin
Posts: 10111
Joined: Sat Feb 28, 2009 10:34 am
cars: TX Gemini 2L Twincam 8psi
TX Gemini SR20 18psi
Datsun 1200 Ute
Subaru Blitzen '06 EZ30 4th gen, 3.0R Spec B
Subaru WRX 2007

Re: Modify E40 OS Routine - CAN

Post by antus »

That is correct, its less preferable because it will take a couple of additional clock cycles to execute. Not normally a problem, but if you can fit it in existing code that is the ideal way, jump out and back is acceptable if needed.
Have you read the FAQ? For lots of information and links to significant threads see here: http://pcmhacking.net/forums/viewtopic.php?f=7&t=1396
User avatar
antus
Site Admin
Posts: 10111
Joined: Sat Feb 28, 2009 10:34 am
cars: TX Gemini 2L Twincam 8psi
TX Gemini SR20 18psi
Datsun 1200 Ute
Subaru Blitzen '06 EZ30 4th gen, 3.0R Spec B
Subaru WRX 2007

Re: Modify E40 OS Routine - CAN

Post by antus »

I just used easy68k to throw together and assemble a snippet to test how many bytes were needed to zero 2 bytes in this way. The result is, that you can clear 1 or 2 (or probably 4) bytes with 4 bytes of code. So it will be possible to use this to claim some space and clock cycles back.
You do not have the required permissions to view the files attached to this post.
Have you read the FAQ? For lots of information and links to significant threads see here: http://pcmhacking.net/forums/viewtopic.php?f=7&t=1396
User avatar
Tre-Cool
Posts: 544
Joined: Tue Oct 16, 2012 2:17 am
cars: VY SS UTE, VX Drag Car
Location: Perth

Re: Modify E40 OS Routine - CAN

Post by Tre-Cool »

Wholy jesus... Awesome replies fella's!

Do i understand any of it? Fck no. lol.

My bad on the title, i just assumed that to modify the code I needed to export the decompiled C code & then re-inject something. Modifying it via assembly kinda makes more sense as that would the actual raw end data vs the precompiled, if my understanding is correct.
User avatar
antus
Site Admin
Posts: 10111
Joined: Sat Feb 28, 2009 10:34 am
cars: TX Gemini 2L Twincam 8psi
TX Gemini SR20 18psi
Datsun 1200 Ute
Subaru Blitzen '06 EZ30 4th gen, 3.0R Spec B
Subaru WRX 2007

Re: Modify E40 OS Routine - CAN

Post by antus »

Well you'd probably have learnt more doing it yourself, and may need to yet, but I couldn't help to write a possible patch in easy68k for you. I ended up with this:

Code: Select all

00001000 Starting Address
Assembler used: EASy68K Editor/Assembler v5.16.01
Created On: 12/11/2025 10:49:54 AM

00000000                             1  *-----------------------------------------------------------
00000000                             2  * Title      :
00000000                             3  * Written by :
00000000                             4  * Date       :
00000000                             5  * Description:
00000000                             6  *-----------------------------------------------------------
00001000                             7      ORG    $1000
00001000                             8  START:                  ; first instruction of program
00001000                             9  
00001000= 00 01 02 03 04 05 ...     10  packet DC.B   $00,$01,$02,$03,$04,$05,$06,$07,$08  ; Define 9-byte array
00001009                            11  
0000100A  43F8 1000                 12          LEA    packet,A1          ; Load the address of buffer into A0
0000100E  1340 0001                 13          MOVE.B D0, 1(A1)          ; Copy the value in D0 into A0 + offset 1
00001012                            14                                    ; --- start patch, code from here goes in the bin at 0x63DFA ---
00001012  1378 0040 0002            15          MOVE.B $40, 2(A1)         ; Copy the value $40 in to A0 + offset 2
00001018  4269 0003                 16          CLR.W  3(A1)              ; Clear bytes at A0 + offset 3, 4
0000101C  4269 0005                 17          CLR.W  5(A1)              ; Clear bytes at A0 + offset 5, 6
00001020  4269 0007                 18          CLR.W  7(A1)              ; Clear bytes at A0 + offset 7, 8
00001024  4E75                      19          RTS                       ; Return
00001026  4E71                      20          NOP                       ; we're 2 bytes shorter than the original, so pad it out
00001028                            21                                    ; --- end patch ---
00001028                            22  
00001028  FFFF FFFF                 23      SIMHALT             ; halt simulator
0000102C                            24  
0000102C                            25  * Put variables and constants here
0000102C                            26  
0000102C                            27      END    START        ; last line of source

No errors detected
No warnings generated
Its putting value 0x40 in offset packet offset 2, and then changing byte to word clear instructions to achieve the same thing in less space. Then it pads out the end of the function after return with a nop (no-operation). This wont be run, its just to be tidy. You could try this as is, and you can change the '40' byte in the byte copy instruction to another byte to get what you need in the can packet. Its untested, but it "might" do what you need. :)

Edit: I feel like i've miscounted something. Ive replaced a clear with a copy that added 2 bytes, and a nop that added 2 bytes. So I should have needed to recover 4 bytes. Which is to replace 2 clear byte opcodes with a single clear word operation, but I've got 3. So exercise for you to check the length of the patch, and if its too short remove a clear word and put clear byte operations back to adjust the length. Or put more NOP opcodes after the RTS to pad it out back up to the original size. You don't need an assembler for this, because nothing is using relative addressing. That means you can move bytes around knowing what they do from the assembly source to modify their function without using an assembler.
Have you read the FAQ? For lots of information and links to significant threads see here: http://pcmhacking.net/forums/viewtopic.php?f=7&t=1396
User avatar
Tre-Cool
Posts: 544
Joined: Tue Oct 16, 2012 2:17 am
cars: VY SS UTE, VX Drag Car
Location: Perth

Re: Modify E40 OS Routine - CAN

Post by Tre-Cool »

So I hit the limit of attachments on my first post last night, which is why there is a double up on the last 2 pics.

This is 1 of the function blocks that references the PN Safety Switch status.
IMS-Function1.png
IMS-Function2.png
Actual location in Ram:
PN-State-Ram.png
You do not have the required permissions to view the files attached to this post.
Last edited by Tre-Cool on Wed Nov 12, 2025 2:32 am, edited 1 time in total.
User avatar
antus
Site Admin
Posts: 10111
Joined: Sat Feb 28, 2009 10:34 am
cars: TX Gemini 2L Twincam 8psi
TX Gemini SR20 18psi
Datsun 1200 Ute
Subaru Blitzen '06 EZ30 4th gen, 3.0R Spec B
Subaru WRX 2007

Re: Modify E40 OS Routine - CAN

Post by antus »

Ok, it'd still be worth patching in what I posted first, just to see if you can see the byte in the output. If you can, then its time to re-visit the patch and add the next piece. You can go for it all at once, but since you don't have a debugger on the PCM baby steps is simpler to identify where the problem is if you get a crash or unexpected results. It doesn't have to be the whole solution in the first attempt. You could even modify the code to use word clears instead of byte clears and pad it out with nops which should result in no functional change at all, just to confirm that it still works as factory and doesn't introduce new problems.
Have you read the FAQ? For lots of information and links to significant threads see here: http://pcmhacking.net/forums/viewtopic.php?f=7&t=1396