Wipe Board with one command

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
lhoff
Posts: 3
Joined: Thu May 26, 2022 9:38 pm

Wipe Board with one command

Post by lhoff » Fri Jul 08, 2022 8:14 pm

Hi,

is there a way to clean the flash of a micropython board with one command. I don't want to reflash the whole flash.
So basically a equivalent to "rm -r /" but remotely on the board.

Why?
Because i want to write some kind of continuous deployment for a showcase. So when a new version of the program is there in want to programmatically take that firmware and transfer it to the micropython board. And to make sure no leftovers stay there i want to wipe everything beforehand.

What i tried already
I tried probably every existing command line program (ampy, mpytool, mpremote, mpr) that isn't interactive (rshell, mpsync, mpfshell). But they all need a concrete filename or directory name.

My first approach was to build upon mpytool since it can be directly imported but the "put" command feels unfinished compared to the rest.

Is there really no way to omit first reading the content, parsing the filelist, walk through the directory tree and delete every file seperately?
I would be really thankful for a hint.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Wipe Board with one command

Post by jimmo » Fri Jul 08, 2022 11:44 pm

lhoff wrote:
Fri Jul 08, 2022 8:14 pm
So basically a equivalent to "rm -r /" but remotely on the board.
You can do this by formatting the block device with the filesystem. Unfortunately right now the exact sequence of commands is different on different ports.

See instructions here for formatting a device as FAT or LittleFS. https://docs.micropython.org/en/latest/ ... ilesystems

I have this in my mpremote config, and it will re-format the device as LittleFS. You could adapt this for FAT based on the docs.

Code: Select all

commands = {
    "littlefs_stm": [
            "exec",
            "--no-follow",
            "import os, pyb, machine; os.umount('/flash'); os.VfsLfs2.mkfs(pyb.Flash(start=0)); os.mount(pyb.Flash(start=0), '/flash'); os.chdir('/flash'); machine.reset()",
    ],
    "littlefs_rp2": [
            "exec",
            "--no-follow",
            "import os, machine, rp2; os.umount('/'); bdev = rp2.Flash(); os.VfsLfs2.mkfs(bdev, progsize=256); vfs = os.VfsLfs2(bdev, progsize=256); os.mount(vfs, '/'); machine.reset()",
    ],
    "littlefs_esp": [
            "exec",
            "--no-follow",
            "import os; os.umount('/'); os.VfsLfs2.mkfs(bdev); os.mount(bdev, '/'); machine.reset()",
    ]
}

lhoff
Posts: 3
Joined: Thu May 26, 2022 9:38 pm

Re: Wipe Board with one command

Post by lhoff » Sun Jul 10, 2022 7:38 pm

Hi,

thanks for the answer. I haven't really though about reformating the device.
When trying it in repl it worked but i have some problems with mpremote.

My understanding from the docs is that "mpremote connect u0" should connect to the board but not start a repl shell. But that is what is always does for me.
Therefore using exec on the command line won't work since i'm, stuck in repl when i execute the commands programatically.

Do you have any idea why that isn't working?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Wipe Board with one command

Post by jimmo » Mon Jul 11, 2022 12:27 am

lhoff wrote:
Sun Jul 10, 2022 7:38 pm
My understanding from the docs is that "mpremote connect u0" should connect to the board but not start a repl shell. But that is what is always does for me.
Therefore using exec on the command line won't work since i'm, stuck in repl when i execute the commands programatically.
mpremote takes a series of commands to execute one after the other, but it has no state across multiple invocations.

The rule is basically that if you don't do a specific control command (e.g. exec, run, fs, etc) then "repl" is added by default.

So "mpremote connect u0" is the same as "mpremote connect u0 repl".

You can also do (for example) "mpremote connect u0 exec "print(1 + 1)" repl".

I think we need to improve the docs for mpremote because the current docs are not very clear on this and it's a bit of an FAQ now :)

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Wipe Board with one command

Post by jimmo » Mon Jul 11, 2022 12:28 am

lhoff wrote:
Sun Jul 10, 2022 7:38 pm
Therefore using exec on the command line won't work since i'm, stuck in repl when i execute the commands programaticall
Not quite sure what you mean here though... if you use

Code: Select all

mpremote connect u0 exec 'python code'
it won't go to the repl.

lhoff
Posts: 3
Joined: Thu May 26, 2022 9:38 pm

Re: Wipe Board with one command

Post by lhoff » Mon Jul 11, 2022 12:42 am

jimmo wrote:
Mon Jul 11, 2022 12:27 am

mpremote takes a series of commands to execute one after the other, but it has no state across multiple invocations.

The rule is basically that if you don't do a specific control command (e.g. exec, run, fs, etc) then "repl" is added by default.

So "mpremote connect u0" is the same as "mpremote connect u0 repl".

You can also do (for example) "mpremote connect u0 exec "print(1 + 1)" repl".
Oh OK got it, thanks.

jimmo wrote:
Mon Jul 11, 2022 12:27 am
I think we need to improve the docs for mpremote because the current docs are not very clear on this and it's a bit of an FAQ now :)
A quick fix would be to add the example from your post to the examples in the docs because there is no other example with connect and a command in it. I was reading the first three examples as "three possible ways to connect"

If I find some time tomorrow I'll create a PR for the Docs so I would understand it ;)

Post Reply