Skip to content

Commit 2ff264d

Browse files
update docs
1 parent bb961db commit 2ff264d

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

docs/os-development/hijack-boot.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,30 @@ You can intercept the boot sequence to drop into a raw REPL shell instead of sta
44

55
## How to Hijack the Boot Sequence
66

7-
Basically, you have to create a file /lib/mpos/main.py
8-
Any code you place there, will be executed by the built-in main.py
7+
Essentially, you have to create a file `/lib/mpos/main.py` on the filesystem.
8+
9+
Code you place there will be executed. If an exception is thrown, it will be printed on the serial port and a REPL shell will be started.
910

1011
## The one-liner
1112

1213
```python
13-
__import__("os").mkdir("/lib") or True; __import__("os").mkdir("/lib/mpos") or True; open("/lib/mpos/main.py","w").write('raise RuntimeError("/lib/mpos/main.py: dropping to REPL shell. To resume boot, do: import mpos.main")\n')
14+
__import__("os").mkdir("/lib") or True; __import__("os").mkdir("/lib/mpos") or True; open("/lib/mpos/main.py","w").write('raise RuntimeError("/lib/mpos/main.py: starting REPL shell. To resume boot, do: import mpos.main")\n')
1415
```
1516

1617
Copy-paste this into your device's REPL (e.g. over serial) and hit enter. The `or True` handles the case where the directories already exist.
1718

18-
## What it does
19+
There are other ways to create this file:
20+
21+
- use mpremote.py to mkdir and then cp the new main.py or
22+
- use a Web IDE like https://fri3dcamp.github.io/Fri3d-IDE/ which have file managers that can create and edit files and folders
23+
24+
## How this works
25+
26+
When the firmware boots, the built-in `main.py` (from `internal_filesystem/main.py` in the repo) adds `/lib` to the start of sys.path and then executes `import mpos.main`.
1927

20-
`/lib/mpos/main.py` is MicroPythonOS's entry point, imported by the frozen `internal_filesystem/main.py`.
28+
Since the internal filesystem's `/lib` is on the sys.path _before_ the .frozen library folder, placing `mpos/main.py` in `/lib` will take precedence and will get executed.
2129

22-
When the firmware boots, it looks for `main.py` on the filesystem — if found, it runs it first. This one-liner writes a stub that raises an error instead, which means:
30+
The one-liner above writes a stub that raises an exception, which means:
2331

2432
1. The device boots into a clean MicroPython REPL.
2533
2. No MicroPythonOS code (LVGL, apps, frameworks) is loaded.
@@ -37,10 +45,8 @@ This runs the full boot sequence (LVGL init, app loading, launcher) as if nothin
3745

3846
## Undoing the hijack
3947

40-
To restore normal boot on every power-on, delete the stub:
48+
To restore normal boot on every power-on, delete the override and its containing folder:
4149

4250
```python
43-
__import__("os").remove("/lib/mpos/main.py")
44-
import shutil
45-
shutil.rmtree("/lib/mpos")
51+
import shutil ; shutil.rmtree("/lib/mpos")
4652
```

0 commit comments

Comments
 (0)