mmio trace for fun and no-profit

I’m not sure what got me interested in assembly language as a nerdy high-schooler. It could have been the growing interest in computer graphics, at a time when you had to use assembly to get decent performance out of the machine. I remember learning tricks from much smarter people than myself, such as how to set a 256x256 pixel video mode so that you could address any pixel without ever needing to multiply. Not that you would use a multiply anyway since you can always use shifts and adds.

I suspect, however, that part of what spurred me on was an interest in reverse-engineering, specifically to crack games. You would start BattleChess, say, and it would ask you for some move from some historical chess game in a big book before letting you play. This was to ensure that if you copied the game, that you at least also photocopied your friend’s manual. [For the record, while I cracked a few games, I never released any such cracks. More from being l4m3 than any teenage sense of ethics. I did, however, release instructions for patching the video game “Home Alone” so that you could never die. I may have been the only person on the planet to play that one all the way through.]

In order to crack a game, first you would load up SoftICE, a killer software debugger that would let you debug almost anything. It would let you set breakpoints on interrupts so step one was to put a breakpoint on ‘int 10h’. Interrupt 0x10 was a call into the video BIOS for setting up the video card. Back in the DOS days, it was the first thing every graphical program (and therefore game) did, because you started out in text mode and had to go into VGA mode. The BIOS would know how to load all the registers for that particular card; you just had to say “put me in 320x200 mode, now, thanks!” and that was via the assembly command “int 10h”.

After SoftICE caught the interrupt, the screen would switch back from VGA mode into a text mode listing of assembly instructions and raw machine code hex values. I don’t think I knew what all these meant at first, but I understood ‘call’ and ‘jmp’, and everything else I quickly learned from a text file describing the x86 instruction set architecture. So cracking then became a matter of just single stepping through all of these instructions, and waiting for BattleChess to get to the part where it asks you the question. Then, you start paying attention, looking for the assembly equivalent of “Is the answer right? If so, goto game! Else goto nasty message!” Some of these were harder than others, but generally it looked something like:

mov ax, ds:[43ac]
mov cx, ds:[3401]
cmp ax, cx
jnz 0027

You could step through it, type the wrong answer, then watch it jump to instruction number 27 which then printed out the nasty message. Then you could try again, and this time modify the code while you’re in the debugger. For example, change the jnz 0027 to a few no-ops (instructions that do nothing). Type in the wrong answer, and now it keeps on going to the game. Bingo, the game is cracked! SoftICE was such a nice debugger that I even used it for C debugging until I finally moved off of that whole DOS thing.

Anyway, I’ll never be a Jon Johansen, but that interest in reversing stuff has stuck with me. I think that figuring out how the Karma worked, which involved many hours of pouring over hex dumps trying to come up with the pattern, was much more fun than coding the driver. Particularly when the “ah-hah!” moments would strike. Incidentally, coding the driver was/is still fun, probably more so than actually using the device.

This little walk down memory lane was inspired by the recent entrance of mmio-trace into my zone of consciousness. I’m not sure who used this first, but the Nouveau project has been using the utility for some time to reverse-engineer the NVidia video cards so that Linux can gain decent open source drivers. My laptop luckily has an Intel based video card, but it does have an Atheros wireless chip which also has a binary blob. There’s currently an effort to produce a reverse-engineered driver for it as well. In my case, the new driver almost worked, but I needed to come up with some other details, so I tried out mmio-trace.

PCI cards are configured by register writes, but the driver generally has to program it to get it going. The BIOS can’t do that work for us any more. So the OS maps a region of memory that the driver can just write into, which then all gets converted magically into writes into the PCI device’s register file. Thus, MMIO=“Memory Mapped IO”, and mmio-trace does what it sounds like. Using it is rather painless, you first ‘hook’ a module you want to capture, modprobe mmiotrace, then load the hooked module. All writes to PCI config space get captured into a debugfs file that you then run through a user-space filter. The end result is a nice report of all of the reads and writes to the registers of the PCI device. Very neat!

Oh, the driver maintainer already knew about the issue and confirmed my proposed change so hopefully by 2.6.26 ath5k will support my Macbook.

February 1, 2008 · bob

I hate you, Sun.

Is it too much to ask of a compiler that if it decides to blare warnings at you for no reason at all, that you can turn the !@#$ thing off? In a move that rivals the stupidity of certain gcc warnings, we have this:

[javac] file: java:519: warning: com.sun.rowset.CachedRowSetImpl is Sun proprietary API and may be removed in a future release

Well, hey that’s a nice heads-up to give me 600 times when compiling our tree. Let me go find the non-com.sun equivalent of that. Oh wait, there isn’t one! And, the freaking javadoc even tells you to do it that way. Okay, well, maybe @SuppressWarnings({"deprecation"}) will work? Nope, haha! Sigh. Time for a wrapper script.

Request of persons who wish to write a better javac: can I have plugins that let me muck with the AST? Custom static checking would be nice…

January 24, 2008 · bob

Back

I am back in the country after wintering abroad. Toronto: still cold.

While away I managed to get some work done on the latest installment of the Rio Karma project: a FUSE-based implementation of the “Optimized” MPEG Filesystem. This one has the advantage that it theoretically will work with the busted every-byte-swapped ReplayTV, and that it takes approximately 10% more time than the kernel driver for a given task. There still isn’t an official release because it will destroy your hard drive and wipe off the serial number so you won’t be able to replace it. Plus it gives another excuse to use git, which has finally replaced svn in my day-to-day.

Meanwhile, work is continuing to suck, with two more people gone this week. So, I’ll be on the market shortly. I’m not sure what I want in a new job but my usual criteria apply: linuxy, interesting, not evil, and convenient commute-wise (I’m about 1 for 4 at current job). I don’t particularly care if it makes millions or not.

Not a whole lot is going on otherwise. Santa brought us a Wii and the wife kicks my ass in tennis. Weird that no matter which controller I use, it’s always the broken one. Just like when playing soccer on the Sega in college…

January 7, 2008 · bob

Gave One Got One


00025.jpg
Originally uploaded by bluesterror

I just received my OLPC from the “Give One Get One” program. For the uninitiated, this is the program that aims to send $100 ($200) laptops to children in undeveloped nations.

First thoughts are that this is a cool little laptop. It is very light. The screen is small but has decent resolution, in the 800x600 range. The processor is an AMD Geode 400 mHZ, it’s got a gig of flash for storage, and 256 megs of RAM. It seems a tad sluggish for a machine with those specs (similar specs to my firewall at home which runs X fine), but not so slow that you couldn’t get used to it.

The UI is neat, running on top of Linux and I understand mostly written in python. There’s a video recording app (did I mention the thing has a camera?), word processor, browser. There’s a little python IDE with some sample programs (basic math, print Pascal’s triangle and Fibonacci numbers, etc). And a terminal where you discover that the device has vim installed but no gcc. C programmers everywhere cry.

All in all, it’s probably not going to work for anyone who already has a laptop, or any adult for that matter without using a USB keyboard since the keys are so small (mine is eventually going to my nephews). But I think if I got this at age 6, it would spark my imagination as much or more than the C=64. I think we’ll see a lot of computer professionals emerging from these countries in 20 years.

(A few more pics at flickr.)

December 21, 2007 · bob

Bread

I made a promise to myself when Cook’s Illustrated began arriving in my mailbox: each month, I have to make at least one recipe from the latest issue. The Jan/Feb issue has an insanely simple bread recipe based on a no-knead recipe from Mark Bittman. I have used his French baguette recipe from How to Cook Everything with much success, but with this recipe, I may just leave the stand mixer in the cupboard from now on.

2 hour cooling time? Ugh! I can’t wait to eat this.

December 16, 2007 · bob

Vista-ed

I got my first look at Vista with Angeline’s new Thinkpad. So far, it’s not looking too good.

December 16, 2007 · bob

December update

The first snow has fallen, and so begins our inevitable slide into winter. Why does winter have to be so cold?

I haven’t been updating this or doing many projects recently, as work has occupied my life. Over the last month or two I’ve been prepping and shipping a 6-month-late, extremely buggy software release and cleaning up the ensuing mess. Unfortunately, I saw this coming a year ago, and continue to see danger ahead: our customer has retaliated by shortening our schedule for phase 2 from six to two months. Those whose managers have never read the Mythical Man Month are doomed to repeat it. Well, it’s about time to start taking off the month of vacation time that I’ve stowed away.

December 8, 2007 · bob

Patitucci @ KC 9:30

It’s been a while since I’ve seen a good band. Well, not too long ago – we saw Los Lonely Boys in Honolulu during our honeymoon. I meant to write that up, but what more is there to say than they are a capable cross between Santana and SRV? Anyway, the fact remains that I’ve seen many fewer shows this year than in recent times.

That is why when I saw that John Patitucci is playing tomorrow night with a 3 piece including the most frickin’ awesome drummer alive, Brian Blade, for only $25 at the Kennedy Center, I had to grab a pair of tickets. (The KC has a jazz club? Who knew?) Patitucci is most well-known as the bass player for various incarnations of Chick Corea bands, and if he doesn’t set the crowd ablaze, I’m sure Blade will.

November 1, 2007 · bob

Halloweekend

Dora and Diego Joe and Jessica can throw a good costume party. As we did 2 years ago, Angeline and I hit up the toons for inspiration. This time we went as Dora the Explorer and Go, Diego, Go!, respectively. Who were just as obscure as Venture Bros. to everyone in attendance. Oh well.

Diego button I didn’t do as much engineering with this one as I did with the Monarch costume, but I did do some craft work: I replaced the black strap on a $10 messenger bag with an orange strap, hemmed Dora’s shorts which were originally jogging pants (yay for my new sewing abilities), and spent an hour or so drawing out the logo for the button to the right in Inkscape. I only saw the logo for a brief period at Toys ‘R Us and then worked from a really low res bitmap, so I was particularly pleased with how that last bit turned out.

I only saw it in pictures, but I think Jerome’s baby in a pot costume was the best there.

October 29, 2007 · bob