Ejemplo n.º 1
0
def wipe():
    """
    Blocks map in disco board
    0: MBR
    1   - 255:   reserved
    256 - 447:   internal flash
    448 - 33215: QSPI
    """
    # delete files normally in simulator
    try:
        delete_recursively(fpath("/flash"))
        delete_recursively(fpath("/qspi"))
    except:
        pass
    # on real hardware overwrite flash with random data
    if not simulator:
        os.umount("/flash")
        os.umount("/qspi")
        f = pyb.Flash()
        block_size = f.ioctl(5, None)
        # wipe internal flash with random bytes
        for i in range(256, 450):
            b = os.urandom(block_size)
            f.writeblocks(i, b)
            del b
            gc.collect()
    # mpy will reformat fs on reboot
    reboot()
Ejemplo n.º 2
0
def wipe_flash_filesystem():
    # erase and re-format the flash filesystem (/flash/)
    import ckcc, pyb
    from main import dis, settings

    dis.fullscreen('Erasing...')
    os.umount('/flash')

    # from extmod/vfs.h
    BP_IOCTL_SEC_COUNT = (4)
    BP_IOCTL_SEC_SIZE = (5)

    # block-level erase
    fl = pyb.Flash()
    bsize = fl.ioctl(BP_IOCTL_SEC_SIZE, 0)
    assert bsize == 512
    bcount = fl.ioctl(BP_IOCTL_SEC_COUNT, 0)

    blk = bytearray(bsize)
    ckcc.rng_bytes(blk)

    # trickiness: actual flash blocks are offset by 0x100 (FLASH_PART1_START_BLOCK)
    # so fake MBR can be inserted. Count also inflated by 2X, but not from ioctl above.
    for n in range(bcount):
        fl.writeblocks(n + 0x100, blk)
        ckcc.rng_bytes(blk)

        dis.progress_bar_show(n * 2 / bcount)

    # rebuild and mount /flash
    dis.fullscreen('Rebuilding...')
    ckcc.wipe_fs()

    # re-store settings
    settings.save()
Ejemplo n.º 3
0
# boot.py - handle safemode, start the watchdog and logging
# Copyright © 2020 by Thorsten von Eicken.

import gc, sys, machine, os, uctypes, stm, pyb

# set a GC threshold early on to reduce heap fragmentation woes
gc.threshold(4096)

# chdir to / to make pybd look like esp32 (ouch!)
os.umount("/flash")
os.mount(pyb.Flash(start=0), "/")
os.chdir("/")
sys.path[:] = ["", "/lib"]


# BkpRAM class copied from https://github.com/peterhinch/micropython-micropower/blob/master/upower.py
# Copyright 2016 Peter Hinch
# This code is released under the MIT licence
class BkpRAM(object):
    BKPSRAM = 0x40024000

    def __init__(self):
        stm.mem32[stm.RCC + stm.RCC_APB1ENR] |= 0x10000000  # PWREN bit
        stm.mem32[
            stm.PWR + stm.
            PWR_CR] |= 0x100  # Set the DBP bit in the PWR power control register
        stm.mem32[stm.RCC + stm.RCC_AHB1ENR] |= 0x40000  # enable BKPSRAMEN
        stm.mem32[stm.PWR +
                  stm.PWR_CSR] |= 0x200  # BRE backup register enable bit
        self._ba = uctypes.bytearray_at(self.BKPSRAM, 4096)
Ejemplo n.º 4
0
pyb.ExtInt(pyb.Pin('B1'), pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_NONE, pwrcb)

# configure usb from start if you want,
# otherwise will be configured after PIN
# pyb.usb_mode("VCP+MSC") # debug mode without USB from start
# disable at start
# pyb.usb_mode(None)
# os.dupterm(None,0)
# os.dupterm(None,1)

# last 512 kB are used for secrets
FLASH_SIZE = 512 * 1024
# check and mount internal flash
if os.statvfs('/flash') == os.statvfs('/qspi'):
    os.umount('/flash')
    f = pyb.Flash()
    numblocks = f.ioctl(4, None)
    blocksize = f.ioctl(5, None)  # 512
    size = numblocks * blocksize
    # we use last 512 kB
    start = numblocks * blocksize - FLASH_SIZE
    if start < 0:
        start = 0
    # try to mount
    try:
        os.mount(pyb.Flash(start=start), '/flash')
    # if fail - format and mount
    except:
        os.VfsFat.mkfs(pyb.Flash(start=start))
        os.mount(pyb.Flash(start=start), '/flash')
# boot.py -- run on boot-up
# can run arbitrary Python, but best to keep it minimal

import machine
import pyb
import os
import utime
pyb.country('GB')  # ISO 3166-1 Alpha-2 code, eg US, GB, DE, AU

# https://pybd.io/hw/pybd_sfxw.html
# The board has a built-in micro SD card slot. If an SD card is inserted, by default it will not be automatically
# mount in the board’s filesystem but it will be exposed as a mass storage device if USB is used. To automatically
# mount the SD card if it is inserted, put the following in your boot.py:
# Enable power supply to sdcard
pyb.Pin.board.EN_3V3.on()
utime.sleep_ms(10)

if pyb.SDCard().present():
    # Extra delay to let the SDCard start up before mounting.
    utime.sleep_ms(500)
    os.mount(pyb.SDCard(), '/sd')
    pyb.usb_mode('VCP+MSC',
                 msc=(pyb.Flash(),
                      pyb.SDCard()))  # act as a serial and a storage device
else:
    pyb.usb_mode('VCP+MSC')  # act as a serial and a storage device

pyb.main('main.py')  # main script to run after this one
Ejemplo n.º 6
0
# MIT license; Copyright (c) 2022 Damien P. George

from micropython import const
import struct, machine, fwupdate, spiflash, pyb

_IOCTL_BLOCK_COUNT = const(4)
_IOCTL_BLOCK_SIZE = const(5)

_SPIFLASH_UPDATE_KEY_ADDR = const(1020 * 1024)
_SPIFLASH_UPDATE_KEY_VALUE = const(0x12345678)

_FILESYSTEM_ADDR = const(0x8000_0000 + 1024 * 1024)

# Roundabout way to get actual filesystem size from config.
# This takes into account the 1M "reserved" section of the flash memory.
flash = pyb.Flash(start=0)
_FILESYSTEM_LEN = flash.ioctl(_IOCTL_BLOCK_COUNT, None) * flash.ioctl(
    _IOCTL_BLOCK_SIZE, None)


def update_app(filename):
    print(f"Updating application firmware from {filename}")

    # Create the elements for the mboot filesystem-load operation.
    elems = fwupdate.update_app_elements(filename, _FILESYSTEM_ADDR,
                                         _FILESYSTEM_LEN)
    if not elems:
        return

    # Create the update key.
    key = struct.pack("<I", _SPIFLASH_UPDATE_KEY_VALUE)
pyb.country('GB')  # ISO 3166-1 Alpha-2 code, eg US, GB, DE, AU


# https://pybd.io/hw/pybd_sfxw.html
# The CPU frequency can be set to any multiple of 2MHz between 48MHz and 216MHz, via machine.freq(<freq>).
# By default the SF2 model runs at 120MHz and the SF6 model at 144MHz in order to conserve electricity.
# It is possible to go below 48MHz but then the WiFi cannot be used.
# From: https://github.com/micropython/micropython/issues/4662
# This sometimes causes problems with USB and possibly SDCard if done later.
# Best done in boot before usb and sdcard are initialised.
# machine.freq(48000000)  # Set to lowest usable frequency

# https://pybd.io/hw/pybd_sfxw.html
# The board has a built-in micro SD card slot. If an SD card is inserted, by default it will not be automatically
# mount in the board's filesystem but it will be exposed as a mass storage device if USB is used. To automatically
# mount the SD card if it is inserted, put the following in your boot.py:
# Enable power supply to sdcard
pyb.Pin.board.EN_3V3.on()
utime.sleep_ms(10)

if pyb.SDCard().present():
    # Extra delay to let the SDCard start up before mounting.
    utime.sleep_ms(500)
    os.mount(pyb.SDCard(), '/sd')
    pyb.usb_mode('VCP+MSC', msc=(pyb.Flash(), pyb.SDCard()))  # act as a serial and a storage device
else:
    pyb.usb_mode('VCP+MSC')  # act as a serial and a storage device

pyb.main('main.py')  # main script to run after this one run after this one