summaryrefslogtreecommitdiff
path: root/ports/zephyr/modules/_boot.py
blob: d0ba21d3f06709be3bf1638221c0ccac97738789 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# ruff: noqa: F821
import sys
import os
from micropython import const
import vfs  # vfs is always available
import zephyr

# FlashArea depends on CONFIG_FLASH_MAP
FlashArea = getattr(zephyr, "FlashArea", None)

# DiskAccess depends on CONFIG_DISK_ACCESS
DiskAccess = getattr(zephyr, "DiskAccess", None)

_FLASH = const("/flash")
_FLASH_LIB = const("/flash/lib")
_STORAGE_KEY = const("storage")
_FLASH_EXISTS = False


def create_flash_partition():
    """Create an LFS2 filesystem on the partition labeled storage
    and mount it on /flash.
    Return True if successful, False otherwise.
    """
    if _STORAGE_KEY in FlashArea.areas:
        bdev = FlashArea(*FlashArea.areas[_STORAGE_KEY])
        retval = True
        try:
            vfs.mount(bdev, _FLASH)
        except OSError:
            try:
                vfs.VfsLfs2.mkfs(bdev)
                vfs.mount(bdev, _FLASH)
            except OSError:
                print("Error formatting flash partition")
                retval = False
        return retval
    return False


def mount_all_disks():
    """Now mount all the DiskAreas (if any)."""
    retval = False
    for da_name in DiskAccess.disks:
        mount_name = f"/{da_name.lower()}"
        da = DiskAccess(da_name)
        try:
            vfs.mount(da, mount_name)
            sys.path.append(f"{mount_name}/lib")
            os.chdir(mount_name)
            retval = True
        except OSError as e:
            print(f"Error mounting {da_name}: {e}")
    return retval


if FlashArea and create_flash_partition():
    _FLASH_EXISTS = True

# Prefer disks to /flash
if not (DiskAccess and mount_all_disks()):
    if _FLASH_EXISTS:
        os.chdir(_FLASH)

# Place /flash/lib last on sys.path
if _FLASH_EXISTS:
    sys.path.append(_FLASH_LIB)

# Cleanup globals for boot.py/main.py
del FlashArea, DiskAccess, zephyr
del sys, vfs, os, const
del create_flash_partition, mount_all_disks, _FLASH_EXISTS