Injecting SD Card Bootloaders

[Frank] has a Ultimaker2 and wanted to install a new bootloader for the microcontroller without having physical access to the circuitry. That means installing a new bootloader for the ATMega2560 without an In System Programmer, and as is usual on AVRs, the bootloader can only be edited with an ISP. Additionally, modifying the bootloader in any way runs the risk of corruption and a bricked circuit. That’s okay, because [Frank] knows how to do it, and he’s here to show you how.

You can think of the memory layout of the ATMega in the Ultimaker as being split in half, with the printer firmware in the first half and the bootloader in the second half. There’s extra space in both halves, and that’s something that comes in very useful. When the circuit powers up, it jumps to the bootloader, does it’s thing, then jumps to the very beginning of the application code – a vector table – that starts up the actual firmware.

[Frank]’s trick to adding on to the bootloader is to place the SD card bootloader in the space normally reserved for applications, not where you would expect to find a bootloader. This code is accessed by the stock bootloader jumping into a modified vector table at the beginning of the application data that points to new executable code. That code is the actual SD card bootloader, but because it is in the application part of the memory, it can’t perform Flash writing or erasing. To fix that, a tiny bit of code is tacked onto the end of the bootloader for performing Flash writes and jumps back to the application part of memory.

12 thoughts on “Injecting SD Card Bootloaders

    1. There won’t be any reset pin access, plus the circuit is 5V and the SD card is behind a unidirectional 3V3 level shifter circuit, so that idea wouldn’t work.

  1. Is there any way to program the controller in an SD card? Specifically for adjusting performance?

    I picked up some cheap 32gig microSD cards, quite obviously NOT the Class 10 they are marked. The best one almost makes it to 3 megabytes/second write speed, and that only after I reformatted it with a program that somehow “found” another megabyte or so, increasing its actual capacity from 29 to just over 30. No longer works in my camera but does in anything else I tried it with.

    Another of the three is at its original capacity and almost matches the “performance” of the rejiggered one. Works in my camera too.

    The third is extremely slow, usually manages under 1meg/sec writes and is prone to spontaneous errors or simply giving up. I tried several times to transfer 24 videos totaling 8.1 gigs and every time it would fail part way through the 22nd. To make it stop I’d have to pull the card and/or forcibly restart Explorer. Then I’d have to do a quick reformat. Nothing wrong with the 22nd video, it copies to any other device, or by itself to the problem card, without problem. I first tried it in my phone, moving everything from the old 16 gig. It was very slow and when “done” there were still many files not moved. I found that there were hundreds of folders created on the new card with gibberish names. Fortunately none of the files actually moved were corrupted and I was able to move them back. I reformatted it then tried it in my camera. I took two pictures (that took a very long time to save) then a card error message popped up. Most likely this card is defekt but if there’s a way to salvage it, even if it’s no faster than the other two, I’d like to do it.

    1. Sounds to me like you have counterfeit cards. All the classic signs there. As for the extra megabyte and the fact it won’t work in your camera now – it probably formatted it with a different cluster size. Even Fat vs. Fat32, etc… will result in slightly different capacities.

      1. Didn’t change the size, it put more on.
        C:\windows\system32>format l: /q
        Insert new disk for drive L:
        and press ENTER when ready…
        The type of the file system is FAT32.
        QuickFormatting 31992M
        Initializing the File Allocation Table (FAT)…
        Volume label (11 characters, ENTER for none)?
        Format complete.
        31.2 GB total disk space.
        31.2 GB are available.

        32,768 bytes in each allocation unit.
        1,023,507 allocation units available on disk.

        32 bits in each FAT entry.

        Volume Serial Number is B00D-0F98

        C:\windows\system32>format l: /q
        Insert new disk for drive L:
        and press ENTER when ready…
        The type of the file system is FAT32.
        QuickFormatting 30716M
        Initializing the File Allocation Table (FAT)…
        Volume label (11 characters, ENTER for none)?
        Format complete.
        30.0 GB total disk space.
        30.0 GB are available.

        32,768 bytes in each allocation unit.
        982,655 allocation units available on disk.

        32 bits in each FAT entry.

        Volume Serial Number is 684C-BC3A

    2. BTW there is a windows program called h2testw.exe It can read/write the SD from a USB reader and report back the write and read speed. Since it verify the write, you can see where it fails as it hits the real capacity.

      I found it reading the discussion on DX on fake SD cards.

  2. What was missing (for me at least) in the description was the reason: “why?” Why would you want to reflash a UM2 that often? And if you should, why is using the USB cable for it too hard? In fact, you do not need ‘access to the electronics’, just a USB B cable in the appropriate socket on the side wouls give you enough access. Perhaps if you wanted to reflash a printer that is not yours, but have no maintenance access to?

    1. I got a new extruder kit called Flex3Drive, it required the motor to spin backwards, a new default extrusion speed, a new default lower initial height for the level routine (the head is lower and I don’t want to crack my glass bed if it hits the nozzle), and a new homing path (it hits the back left cover because the head is lower). None of these changes exist yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.