|
| 1 | +def bios(): |
| 2 | + globals().pop('bios') |
| 3 | + |
| 4 | + from machine import invoke, components, crash |
| 5 | + |
| 6 | + eeproms = components("eeprom") |
| 7 | + if not eeproms: |
| 8 | + crash("no bios found; install a configured EEPROM with Python") |
| 9 | + |
| 10 | + eeprom = eeproms[0] |
| 11 | + if True: |
| 12 | + invoke(eeprom, 'setLabel', "EEPROM (micropython)") |
| 13 | + invoke(eeprom, 'set', b"""python = [] |
| 14 | +from machine import invoke, components, crash |
| 15 | +from uio import FileIO |
| 16 | +
|
| 17 | +
|
| 18 | +init = '/init.py' |
| 19 | +
|
| 20 | +
|
| 21 | +filesystems = components("filesystem") |
| 22 | +def check_bootable(address): |
| 23 | + return address and address in filesystems and invoke(address, 'exists', init) |
| 24 | +
|
| 25 | +
|
| 26 | +def load(address): |
| 27 | + file = invoke(address, 'open', init, 'r') |
| 28 | + |
| 29 | + try: |
| 30 | + buffer = [] |
| 31 | + while True: |
| 32 | + buf = invoke(address, 'read', file, 4096) |
| 33 | + if not buf: break |
| 34 | + buffer.append(buf) |
| 35 | + finally: |
| 36 | + invoke(address, 'close', file) |
| 37 | + |
| 38 | + content = b"".join(buffer) |
| 39 | + return content.decode() |
| 40 | +
|
| 41 | +
|
| 42 | +def main(): |
| 43 | + address = invoke(__path__, 'getData').decode() |
| 44 | + if not check_bootable(address): |
| 45 | + invoke(__path__, 'setData', b'') |
| 46 | + for address in filesystems: |
| 47 | + if check_bootable(address): |
| 48 | + break |
| 49 | + else: |
| 50 | + crash("no bootable medium found") |
| 51 | +
|
| 52 | + content = load(address) |
| 53 | + context = {'__name__': '__main__', '__path__': address} |
| 54 | + func = compile(content, "<init>", "exec") |
| 55 | + exec(content, context) |
| 56 | +
|
| 57 | +
|
| 58 | +if __name__ == '__main__': |
| 59 | + main() |
| 60 | +""") |
| 61 | + |
| 62 | + data = invoke(eeprom, 'get') |
| 63 | + context = {'__name__': '__main__', '__path__': eeprom} |
| 64 | + |
| 65 | + try: |
| 66 | + exec(data, context) |
| 67 | + except BaseException as e: |
| 68 | + print(type(e), e) |
| 69 | + |
| 70 | + |
| 71 | +bios() |
0 commit comments