cyrozap / jmb58x-re

Notes and utilities for reverse engineering JMicron's JMB582 / JMB585 SATA host controllers.
BSD Zero Clause License
9 stars 1 forks source link

Flash dumper tool? #1

Open fieryo opened 1 year ago

fieryo commented 1 year ago

I appreciate your research on JMB585 chip, but would you share with us how to dump the flash from a PCIe card (point 2 of README). I have a PCIe 3.0 x1 card with JMB582, that works with CSM ("UEFI only"), but refuses to load when "Secure boot" is on. This means that either the firmware is not signed or it's not the original from JMicron. At least, I want to verify that it's the original firmware. If you share some links to the original firmware would be great as well.

Thanks,

cyrozap commented 1 year ago

If I understand your question correctly, you just want to dump the Option ROM from the card. If that's the case, you don't need a full dump of the flash chip (which is what I was referring to in the Readme, and at this time requires either an SPI flash programmer or a logic analyzer to perform) in order to solve your problem. The Option ROM (the firmware you're referring to) is exposed to the operating system in a standard way, so if you're running Linux (don't ask me how to do this on Windows--I don't know), you can dump it from the command line using standard Linux tools. The process to do this is as follows:

First, list the JMB58x devices in your system:

$ lspci -Dd 197b:
0031:04:00.0 SATA controller: JMicron Technology Corp. JMB58x AHCI SATA controller

Then, taking the NNNN:NN:NN.N (domain:bus:slot.function) number you got from the output of the previous command, write a "1" to the device's "rom" file in sysfs:

$ echo 1 | sudo tee '/sys/bus/pci/devices/0031:04:00.0/rom'

This enables reading the Option ROM from the card, which you can do with the following command:

$ sudo cat '/sys/bus/pci/devices/0031:04:00.0/rom' > jmb58x.rom

With this last step completed, you should have the entire option ROM in a file called jmb58x.rom. Unfortunately, I don't know enough about UEFI to know how to parse and verify that image, so you'll have to find that out on your own. Fortunately, the UEFI specs are open, so that should "simply" be a matter of reading the relevant documentation and writing some code to do that, or finding a tool that someone else has made to do it.

I hope this helps!


Some extra information on the flash image format and dumping methods, in case you (or anyone else reading this) are curious:

As I explained in my notes, the flash image contains more than just the Option ROM. Just before the Option ROM is a sequence of configuration instructions that write to registers inside the chip, to control things like the GPIO pin configurations (e.g., for LED control) and the offset of the Option ROM in the flash chip. As far as I can tell, the JMB58x chip itself doesn't load or execute any firmware.

To read or write this configuration data, and to write to the Option ROM, you need to be able to read and write the whole flash chip. Unfortunately, I'm not aware of any method to read or write the entire flash of these cards over PCIe. While there's probably a way to do it, I haven't been able to find any flashing programs (commonly referred to as "MP tool" or "MPTool") for these cards anywhere, and it would be extremely difficult to reverse engineer that process without one.

Lacking a method to dump the flash over PCIe, in the beginning I just connected a logic analyzer to the SPI flash pins and sniffed the communications--that let me see both the flash contents and access patterns. Later, to get a more thorough dump, I desoldered the flash and stuck it in a cheap SPI flash programmer. So, for now, these are the only methods I know of that can be used to read (and, in the case of the SPI flash programmer, also write) the flash beyond just the Option ROM.

fieryo commented 1 year ago

Thank you, that was really helpful.

Do you know if JMB585 (i.e. the one you dumped) is signed and loads with Secure boot?

In my case (after I dump the OpROM in Linux), even a checksum of the original JMB582 OpROM would be enough to verify it. But I'm unable to find such firmware or jmb58x.rom file. Station-drivers has JMB585 drivers only. Do you know some forum, where other users dump firmwares, so we can compare our checksums?

cyrozap commented 1 year ago

I have a few dumps and checksums:

I think only the 36864-byte image is an EFI Option ROM, since it's the only one that matches this header format--the others look like legacy BIOS Option ROMs. And I have no idea if any of these are signed or working with UEFI secure boot, as the system this card is installed in doesn't support UEFI or loading Option ROMs, and I don't know how to find and verify the signature manually.