WIP [Trinket] [Rebug] [Others] SWITCHBOOT_UF2 / FUSEE_UF2 modchip software

Discussion in 'Switch - Backup Loaders & Modchips' started by mattytrog, Dec 18, 2018.

  1. MatinatorX

    MatinatorX DragonInjector Project Creator

    Member
    8
    Jul 17, 2018
    Canada
    @mattytrog Hey buddy, been a while since I poked my head in here, looking great!

    Wanted to stop by to see if the recent work done for the DI might be useful for you as well. I'm not completely caught up on everything your firmware does, but I do see two things I think I can help with:

    1) Flash storage of settings. I noticed you're using the FlashStorage library with the alternate EEPROM functions. While the EEPROM calls are a little more verbose than the default functions, it still doesn't do any wear leveling of any kind, and the SAMD21 flash area has a fairly low rated cycle count (min 25k, average 150k cycles). In addition, FlashStorage is a bit of a space hog, though with your 256kb of flash area compared to my 64kb, maybe that's not an issue for you. :P

    To combat this I wrote (and optimized with the help of stuckpixel) a 16 "bin" wear-leveling algorithm for user settings. It works as such, with byte 1 of each bin indicating a full or empty bin:

    Code:
    READING VALUES:
    1) Code checks last row page 4, if byte 1 is > 0 then it loads the values, if not then:
    2) Code checks last row page 3, if byte 1 is > 0 then it loads the values, if not then:
    3) ...
    15) Code checks fourth last row page 2, if byte 1 is > 0 then it loads the values, if not then:
    16) Code checks fourth last row page 1, if byte 1 is > 0 then it loads the values, if not then it sets default values.
    
    WRITING VALUES:
    1) Check fourth last row page 1, if byte 1 == 0 then write the values and set byte 1 > 0, if not then:
    2) Check fourth last row page 2, if byte 1 == 0 then write the values and set byte 1 > 0, if not then:
    3) ...
    15) Check last row page 4, if byte 1 == 0 then write the values and set byte 1 > 0, if not then:
    16) Erase last four rows, write the values and set byte 1 > 0 at fourth last row page 1.
    This brings our rated cycle count up to min 400k, average 2.4M cycles, at the cost of the last 1kb of flash area. The code takes advantage of the fact we have row erase granularity but page write granularity with SAMD21. With SAMD21, each page is 64 bytes, so that's how much we have for data storage. It looks like you're currently using 41 bytes for data storage, and a lot of those uint32_t's look like they could be uint8_t's, so you would have lots of room for extra settings. ;)

    Plus, since we're using defined memory locations, all your user settings will persist through both user code and bootloader updates, so long as your flash area has at least 1k of free space.

    The source for it is here: https://github.com/dragoninjector-project/DragonInjector-Firmware/blob/master/main/main.ino

    2) A CLI. I'm sure you're aware that the UF2 bootloader can emulate a USBSerial device and does so by default for BOSSA. However, I saw this as an opportunity for easier configuration of user settings and/or an easier way for apps to configure them. Rather than poking and prodding CURRENT.UF2 (which I don't think would work for FlashStorage anyways without a magic number) we can just talk plaintext. This potentially opens up the door for fancy Homebrew right on-console which can configure our modchips - I've been told support for USB Serial would be far easier to implement than FAT16 MSC. Pretty much everything already has it. Attached is an image of me configuring a DI with my phone, and Jerome is working hard on adding a nice configuration screen to the Windows app.

    Source for the CLI: https://github.com/dragoninjector-project/DragonInjector-Bootloader/blob/master/src/sam_ba_monitor.c
    Source for the Windows app: https://github.com/dragoninjector-project/DragonInjector-UpdateTool

    Anyways, let me know what you think! :grog:

    CONFIG. Screenshot_20191108-130452.
     
    Last edited by MatinatorX, Nov 9, 2019 at 12:15 AM
  2. BizkutPhilly

    BizkutPhilly Banned

    Banned
    1
    Friday
    United States
    same issue here
     
  3. mattytrog
    OP

    mattytrog You don`t want to listen to anything I say.

    Member
    13
    Apr 27, 2018
    United Kingdom
    Will take a look!

    Switchboot is due for some updates. I`ve done some experiments with wear-levelling and I did build a kind-of implementation into the UF2 bootloader. But I faced a few issues. And couldn`t be arsed to work them out. And it was probably crap...

    You can poke and prod flashstorage with CERTAIN functions - by no means all.

    I`ll look at your implementation and see what we can implement this side. FlashStorage was a quick and dirty way of accomplishing what I needed to at the time.

    I`ve been doing work on FatFS and mounting (with tweak) the OS partitions from within the modchip software, permitting things like proper emmc cleaning, decrypting changing / re-encrypting prodinfo, full resets etc etc. Also, limited testing of chips (BQ24193 etc) by using i2c command set to attempt to diagnose problems.
    For the most part, its fully working. I just don`t trust myself to not open a can of worms with it. It is powerful and could irretrievably kill your console. In fact, in the wrong hands, it certainly will.

    At Skegness at the moment for the Beach races, but will get on it later.

    So, yep... Probably aim for a 2.0 release with some powerful functions. Just nerves getting the better of me.

    Good work sir!

    Matty
     
    MatinatorX likes this.
  4. mattytrog
    OP

    mattytrog You don`t want to listen to anything I say.

    Member
    13
    Apr 27, 2018
    United Kingdom
    @MatinatorX

    Looked at your implementation of storage.

    I like it.

    A lot. Very clean.
     
    MatinatorX likes this.
  5. mattytrog
    OP

    mattytrog You don`t want to listen to anything I say.

    Member
    13
    Apr 27, 2018
    United Kingdom
    Implementing your wear-levelling code today ;)

    Hiding the settings right at the end of the flash area, into a struct.
    Commit the write just before a NVIC_SystemReset.

    Working well. Cheers! ;)
     
  6. Joseph111

    Joseph111 Member

    Newcomer
    1
    Oct 9, 2019
    United Kingdom
    i got my thing working now, cheers btw and what is this? Does it do anything differently, sorry I don't understand all of what you and Matin spoke about.
     
  7. mattytrog
    OP

    mattytrog You don`t want to listen to anything I say.

    Member
    13
    Apr 27, 2018
    United Kingdom
    See below Joseph - may help you?

    @Everybody
    OK. Some new features of next "official" release... This won`t be a testing release like v1.5.1 was

    • Access UF2 bootloader with vol+ switch. No need to flex covers or hold stupid reed switches to your back case any more. Just a single (or double - haven`t decided yet) press.
    • Wear-levelling added to the firmware, to vastly enable more writes / rewrites. Thanks @MatinatorX!
    • Flat battery assistance - If battery is flat, Switchboot will stay running until battery is sufficiently charged to enable a full boot, rather than bootloop. This will assist perma-CFW / autoRCM users.
    • Auto-power-off if USB unplugged. It used to launch to Hekate / Switchboot screen and stay there(usb strap users only)
    • Bugfixes and obsolete libraries / functions removed. Started from scratch effectively...
    • More coming. But VERY VERY busy with consoles, so a little is getting done every night.

    So may be a day or two before its ready
     
    Last edited by mattytrog, Nov 14, 2019 at 12:06 AM
    MatinatorX likes this.
  8. Joseph111

    Joseph111 Member

    Newcomer
    1
    Oct 9, 2019
    United Kingdom
    Access UF2 bootloader with vol+ switch. No need to flex covers or hold stupid reed switches to your back case any more

    How does that work? Does Switch need to be turned on? I'm using Vol+ to launch into CFW why I ask. I understand the rest
     
  9. mattytrog
    OP

    mattytrog You don`t want to listen to anything I say.

    Member
    13
    Apr 27, 2018
    United Kingdom
    At any point when the Switch is powered on( or ideally you have sent the SAMD21_Update payload / selected the option in menu), hold vol+ for a second or two to bring up SWITCHBOOT.

    We normally need to flex the back cover or hold a magnet (twice) to the back cover -(RCMX86 users).

    I can now fit "hidden" chips, which live underneath the switch mainboard. If you look on my Youtube, you will see a video of a "modchip-less" console. (it isn`t really modchip-less, its underneath, fitted directly to the Switch PCB)
     
    Last edited by mattytrog, Nov 14, 2019 at 12:13 AM
  10. Joseph111

    Joseph111 Member

    Newcomer
    1
    Oct 9, 2019
    United Kingdom
    oh that is actually cool. probably the best thing in the next update
     
    mattytrog likes this.
  11. mattytrog
    OP

    mattytrog You don`t want to listen to anything I say.

    Member
    13
    Apr 27, 2018
    United Kingdom
    Hekate upstream updates will be there too (once I have been through them).

    This isn`t meant to be a rocketship or anything exciting. Its just a "modchip" after all. But time these loose ends were tied up :)
     
    Joseph111 likes this.
  12. MatinatorX

    MatinatorX DragonInjector Project Creator

    Member
    8
    Jul 17, 2018
    Canada
    Glad to hear it helped! :D
     
Quick Reply
Draft saved Draft deleted
Loading...