diff --git a/Payload2UF2/main/uf2conv.py b/Payload2UF2/main/uf2conv.py new file mode 100755 index 0000000..d12c610 --- /dev/null +++ b/Payload2UF2/main/uf2conv.py @@ -0,0 +1,300 @@ +#!/usr/bin/env python3 +#command python uf2conv.py -o end_file_name.uf2 start_file_name.bin +import sys +import struct +import subprocess +import re +import os +import os.path +import argparse + + +UF2_MAGIC_START0 = 0x0A324655 # "UF2\n" +UF2_MAGIC_START1 = 0x9E5D5157 # Randomly selected +UF2_MAGIC_END = 0x0AB16F30 # Ditto + +families = { + 'SAMD21': 0x68ed2b88, + 'SAMD51': 0x55114460, + 'NRF52': 0x1b57745f, + 'STM32F1': 0x5ee21072, + 'STM32F4': 0x57755a57, + 'ATMEGA32': 0x16573617, +} + +INFO_FILE = "/INFO_UF2.TXT" + +appstartaddr = 0x2000 +familyid = 0x0 + + +def is_uf2(buf): + w = struct.unpack(" 476: + assert False, "Invalid UF2 data size at " + ptr + newaddr = hd[3] + if curraddr == None: + appstartaddr = newaddr + curraddr = newaddr + padding = newaddr - curraddr + if padding < 0: + assert False, "Block out of order at " + ptr + if padding > 10*1024*1024: + assert False, "More than 10M of padding needed at " + ptr + if padding % 4 != 0: + assert False, "Non-word padding size at " + ptr + while padding > 0: + padding -= 4 + outp += b"\x00\x00\x00\x00" + outp += block[32 : 32 + datalen] + curraddr = newaddr + datalen + return outp + +def convert_to_carray(file_content): + outp = "const unsigned char bindata[] __attribute__((aligned(16))) = {" + for i in range(len(file_content)): + if i % 16 == 0: + outp += "\n" + outp += "0x%02x, " % ord(file_content[i]) + outp += "\n};\n" + return outp + +def convert_to_uf2(file_content): + global familyid + datapadding = b"" + while len(datapadding) < 512 - 256 - 32 - 4: + datapadding += b"\x00\x00\x00\x00" + numblocks = (len(file_content) + 255) // 256 + outp = b"" + for blockno in range(numblocks): + ptr = 256 * blockno + chunk = file_content[ptr:ptr + 256] + flags = 0x0 + if familyid: + flags |= 0x2000 + hd = struct.pack(b"= 3 and words[1] == "2" and words[2] == "FAT": + drives.append(words[0]) + else: + rootpath = "/media" + if sys.platform == "darwin": + rootpath = "/Volumes" + elif sys.platform == "linux": + tmp = rootpath + "/" + os.environ["USER"] + if os.path.isdir(tmp): + rootpath = tmp + for d in os.listdir(rootpath): + drives.append(os.path.join(rootpath, d)) + + + def has_info(d): + try: + return os.path.isfile(d + INFO_FILE) + except: + return False + + return list(filter(has_info, drives)) + + +def board_id(path): + with open(path + INFO_FILE, mode='r') as file: + file_content = file.read() + return re.search("Board-ID: ([^\r\n]*)", file_content).group(1) + + +def list_drives(): + for d in get_drives(): + print(d, board_id(d)) + + +def write_file(name, buf): + with open(name, "wb") as f: + f.write(buf) + print("Wrote %d bytes to %s." % (len(buf), name)) + + +def main(): + global appstartaddr, familyid + def error(msg): + print(msg) + sys.exit(1) + parser = argparse.ArgumentParser(description='Convert to UF2 or flash directly.') + parser.add_argument('input', metavar='INPUT', type=str, nargs='?', + help='input file (HEX, BIN or UF2)') + parser.add_argument('-b' , '--base', dest='base', type=str, + default="0x2000", + help='set base address of application for BIN format (default: 0x2000)') + parser.add_argument('-o' , '--output', metavar="FILE", dest='output', type=str, + help='write output to named file; defaults to "flash.uf2" or "flash.bin" where sensible') + parser.add_argument('-d' , '--device', dest="device_path", + help='select a device path to flash') + parser.add_argument('-l' , '--list', action='store_true', + help='list connected devices') + parser.add_argument('-c' , '--convert', action='store_true', + help='do not flash, just convert') + parser.add_argument('-D' , '--deploy', action='store_true', + help='just flash, do not convert') + parser.add_argument('-f' , '--family', dest='family', type=str, + default="0x0", + help='specify familyID - number or name (default: 0x0)') + parser.add_argument('-C' , '--carray', action='store_true', + help='convert binary file to a C array, not UF2') + args = parser.parse_args() + appstartaddr = int(args.base, 0) + + if args.family.upper() in families: + familyid = families[args.family.upper()] + else: + try: + familyid = int(args.family, 0) + except ValueError: + error("Family ID needs to be a number or one of: " + ", ".join(families.keys())) + + if args.list: + list_drives() + else: + if not args.input: + error("Need input file") + with open(args.input, mode='rb') as f: + inpbuf = f.read() + from_uf2 = is_uf2(inpbuf) + ext = "uf2" + if args.deploy: + outbuf = inpbuf + elif from_uf2: + outbuf = convert_from_uf2(inpbuf) + ext = "bin" + elif is_hex(inpbuf): + outbuf = convert_from_hex_to_uf2(inpbuf.decode("utf-8")) + elif args.carray: + outbuf = convert_to_carray(inpbuf) + ext = "h" + else: + outbuf = convert_to_uf2(inpbuf) + print("Converting to %s, output size: %d, start address: 0x%x" % + (ext, len(outbuf), appstartaddr)) + if args.convert or ext != "uf2": + drives = [] + if args.output == None: + args.output = "flash." + ext + else: + drives = get_drives() + + if args.output: + write_file(args.output, outbuf) + else: + if len(drives) == 0: + error("No drive to deploy.") + for d in drives: + print("Flashing %s (%s)" % (d, board_id(d))) + write_file(d + "/NEW.UF2", outbuf) + + +if __name__ == "__main__": + main() diff --git a/Payload2UF2/main/uf2conv_mac b/Payload2UF2/main/uf2conv_mac deleted file mode 100755 index acc0686..0000000 Binary files a/Payload2UF2/main/uf2conv_mac and /dev/null differ diff --git a/README.md b/README.md index 4f48b9e..393c9a8 100644 --- a/README.md +++ b/README.md @@ -1,94 +1,87 @@ # SwitchME - -******** This is NOT an Piracy device to circumvent anti-piracy mesures ******** -******** The SiwtcME comes blank and YOU choose what to install on it ******** - All the SwitchME does is make it more convient to launch a payload -So you do not have to connect up a usb cable to your computer and launch your payload - There is no way to pirate anything with the SwithME - ******** Again the SwitchME is not a circumvention device! ******** -_________________________________________________________________________________________ - -*** If you have Auto RCM enabled DISABLE it until you flash the SwitchME for the first time (Use CTCaer 4.1 *** - -*** If you flash any other payload other than CTCaer 4.1 after enabling Auto RCM you will not have a way to flash -the SwitchME unless you can boot into Horizon because there is no usb data comms while in RCM (for now) *** - -*** If you get stuck because of Auto RCM and only booting to say BISkeydump, the way around this is to disconnect either D+ or D- Boot into Horizon, reconnect the line and than flash the SwitchME bootloader *** - -*** Again this is only if you have Auto RCM enabled *** - -*** We highly suggest you only use CTCaer 4.1 and put whatever payload you want to boot in the payloads dir from the SDCard files *** - -*** We have included some for you *** - -After Wiring in the SwitchME you can access the bootloader by conecting a usb-c cable directly to the switch. -Make sure you have a compliant ucb-c cable first though! -A compliant cable will have a 56k Ohm resistor (check where you perchased your cable from) -Really shouldnt be an issue if you are just flashing the SwitchMe directly from your computer, but be on the safe side as plenty of people have burned up their charge IC using 3rd party docks and cheap usb-c cables. - +## Preamble ## +**This is NOT an Piracy device to circumvent anti-piracy mesures** +**The SwitchME comes blank and YOU choose what to install on it** + +The SwitchMe is intended to have a more convient way to launch a payload on your device. +So you do not have to connect a usb cable to your computer and launch your payload. +There is no way to pirate anything with the SwithME. +**Again the SwitchME is not a circumvention device!** + +## Important informations ## +* If you have Auto RCM enabled DISABLE, it until you flash the SwitchME for the first time (Use HekateCTCaer) +* If you flash any other payload other than Hekate CTCaer after enabling Auto RCM you will not have a way to flash +the SwitchME unless you can boot into Horizon because there is no usb data comms while in RCM (for now) +* If you get stuck because of Auto RCM and only booting to say BISkeydump, the way around this is to disconnect either D+ or D- Boot into Horizon, reconnect the line and than flash the SwitchME bootloader +* Again this is only if you have Auto RCM enabled +* We highly suggest you only use Hekate CTCaer and put whatever payload you want to boot in the payloads directory of hekate. +* After Wiring in the SwitchME you can access the bootloader by conecting a USB-C cable directly to the switch. Make sure you have a compliant USB-C cable first though! +* A compliant cable will have a 56k Ohm resistor (check where you perchased your cable from). Really shouldnt be an issue if you are just flashing the SwitchMe directly from your computer, but be on the safe side as plenty of people have burned up their charge IC using 3rd party docks and cheap usb-c cables. + +## Preflashing from a Computer ## If you want to preflash the SwitchME connect a usb cable directly to the the board (D+ green) (D- white) (5v red) (gnd black) On first connect to your computer it will already be in bootloader mode ready to be flashed. After you have flashed the device for the very first time you will need to double press the button to go into bootloader mode again if you want to reflash the device, or if you connect the reset line you can do the same. - -_________________________________________________________________________________________ +Also don't get confused. As soon as you successfully copied the *\*.uf2* file onto your SwitchMe device it will stop serving the disk and you don't have a change to successfully eject/umount it. It's the usual behaviour and everything should work as expected. For Pre-Compiled payloads check UF2_SwitchME_Payloads -_________________________________________________________________________________________ - +## Compile payloads on your own ## To compile payloads for SwitchME you will need to install the Arduino IDE https://www.arduino.cc/en/Main/Software +### Install the board manager for the SwitchME -~ Next you must install the board manager for SwitchME ~ - -Open Arduino IDE > Preferances > Additional Boards Manager URLs > Add The URL Below -https://raw.githubusercontent.com/Aboshi/SwitchME/master/package_switchme_index.json +1. Open Arduino IDE > Preferances > Additional Boards Manager URLs > Add The URL Below ```https://raw.githubusercontent.com/Aboshi/SwitchME/master/package_switchme_index.json``` +1. Next go to *Tools > Board > Boards Manager > Type SAMD > Select Arduino SAMD Boards* and Install version 1.6.21 +1. Next search for *rebug* and install it +1. You can now select *SwitchME m0* in the board programmer -Next go to Tools > Board > Boards Manager > Type SAMD > Select Arduino SAMD Boards and Install version 1.6.21 +### Compiling the payload ### +To compile different payoads you must first convert your payload.bin to hex +The easiest way is to use binConverter.py (you can find it in *Payload2UF2/main*). +Just use the following command line ```python2 binConverter.py ```. You need python2 for it. +A ```.h``` should be generated. -Next search for rebug and install +Next open *SwitchME.ino* in *Payload2UF2/main* in the arduino application. +Change *ctcaer\_4.0\_hekate.h* in the arduino sketchbook/project to whatever you named you payload. Make sure ```.h``` is in the same directory as the *SwitchMe.ino* file. +If you want to upload directly to your SwitchME just click on upload (your SwitchME is ready for use). +If you want to compile to make a .bin for converting to UF2 select *Sketch > Export compiled Binary*. -You can now select SwitchME m0 in the board programmer +The final step is to convert the compiled *\*.bin* into the UF2 format. You can do this with *uf2conv.py* also located in *Payload2UF2/main*. +In this case you need python3. Here is the command line examle. ```python3 uf2conv.py -c main.ino.rebug_m0.bin```. Make sure the filename matches the generated *\*.bin*. A file named *flash.uf2* should be generated. -_________________________________________________________________________________________ +We try to keep all important payloads up to date. So in most cases you don't need to create your own *\*.uf2* files. -To compile different payoads you must first convert your payload.bin to hex -The easiest way is to use binConverter.py (you can find it under releases) -Open SwitchME.ino -Change ctcaer_4.0_hekate.h to whatever you named you payload -If you want to upload directly to your SwitchME just click on upload (your SwitchME is ready for use) -If you want to compile to make a .bin for converting to UF2 select Sketch > Export compiled Binary - -You can find all the tools you need in the releases section of this git as well as pre compiled payloads in UF2 format -We will provide plenty of payloads in UF2 format and keep them updated with new releases. - -_________________________________________________________________________________________ - -Wiring Diagrams: -Check wiring dir -4_wire_always_on (Do not use this method any longer) Leaving it in the git for reference only -4_wire_on_at_powerup (when used with auto rcm you will get instant bootup to whatever you flahed the SwitchME with) We highly suggest CTCaer payloads! -More diagrams will be added soon with auto rcm line strapping, emmc cutoff (another auto rcm) and many others. +## Wiring Diagrams ## +Check the directory *wiring/4\_wire\_on\_at\_powerup* (when used with auto rcm you will get instant bootup to whatever you flahed the SwitchME with) We highly suggest CTCaer payloads! +More diagrams could be added soon with auto rcm line strapping, emmc cutoff (another auto rcm) and many others. For now you can check this thread for other wiring methods that suit your needs: https://gbatemp.net/threads/internal-modchip-samd21-trinket-m0-gemma-m0-itsybitsy-m0-express-guide-files-support.508068/ Huge thanks to @mattytrog for adding the SwitchMe to the current list! -_________________________________________________________________________________________ -Custom Firmware and Emulators: +## Generic Informations +The SwitchME also has 3.5mb of storage space. You can access it by putting the SwitchME into bootloader mode (double press the button). -Roll your own: +## Credits ## +### Custom Firmware and Emulators ### + +Create your own customized CFW and choose all included compontents: https://www.sdsetup.com/ -^^ -Thanks to @noahc3 & @tomGER +Thanks to @noahc3, @tomGER and all other contributers Or Download an AIO package: -https://github.com/tumGER/SDFilesSwitch/releases -^^ Tkanks to @tomGER for keeping things up to date -_________________________________________________________________________________________ +https://github.com/Team-Neptune/DeepSea/releases +Thanks to @Team-Neptune for keeping things up to date -The SwitchME also has 3.5mb of storage space. You can access it by putting the SwitchME into bootloader mode (double press the button) +Also have a look at this homebrew to automatically update all components (except the payload for the SwitchMe) automatically directly from the Switch. +https://github.com/HamletDuFromage/aio-switch-updater/releases +### Switch Scene ### Greets to all the devs in the Switch scene without them none of this would be possible: @SciresM @hexkyz @naewhert @oct0xor @ktemkin @CTCaer @rajkosto @Reisyukaku @atlas44 @noemu @st4rk and countless others + +### tools used ### +* [Euclala](https://github.com/euclala/) for the *binConverter.py* +* [Microsoft](https://github.com/microsoft/) for the *uf2conv.py*