Flashing with frozen main.py and boot.py



  • Hello,

    I have a question about flashing a custom firmware.

    Recently I have been freezing my modules and flashing the firmware to save memory which has been working great. However, I realized that if I freeze main.py or boot.py they get overwritten by the default empty files. Is there a way around this?

    To elaborate on my general goal:

    I intend to use OTA to update my scripts with the pycom OTA module, since the boot and main don't freeze, I would have to upload them through ftp which defeats the purpose of writing a script to automatically install new updates through the pycom module.

    I could also be tackling this the wrong way so any feedback is appreciated.

    Thanks in advance.



  • @robert-hh Thanks! I tried this and it works great! I tried also the new update with the freezing the _main.py which also solves the problem.



  • @yahia If you build your own firmware images anyhow, you can also modify the startup behaviour. The point to look at if the file esp32/mptask.c, line 226 and the following. You see a statement:
    pyexec_frozen_module("_boot.py");
    which executes _boot.py as frozen module, and then later the stamements:
    int ret = pyexec_file("boot.py"); and
    int ret = pyexec_file(main_py);
    which start boot.py and main.py (or whatever it's told to be) as files. You can modify that to your needs. Especially if you want to run your own code from frozen bytecode, you may replace pyexex_file() by pyexec_frozen_module(). I would not do thing outside the safeboot clause. See the section I talked about below:

        pyexec_frozen_module("_boot.py");
    
        if (!safeboot) {
            // run boot.py
            int ret = pyexec_file("boot.py");
            if (ret & PYEXEC_FORCED_EXIT) {
                goto soft_reset_exit;
            }
            if (!ret) {
                // flash the system led
                mperror_signal_error();
            }
        }
    
        if (!safeboot) {
            // run the main script from the current directory.
            if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
                const char *main_py;
                if (MP_STATE_PORT(machine_config_main) == MP_OBJ_NULL) {
                    main_py = "main.py";
                } else {
                    main_py = mp_obj_str_get_str(MP_STATE_PORT(machine_config_main));
                }
                int ret = pyexec_file(main_py);
                if (ret & PYEXEC_FORCED_EXIT) {
                    goto soft_reset_exit;
                }
                if (!ret) {
                    // flash the system led
                    mperror_signal_error();
                }
            }
        }
    


  • Thank you guys for the input, I have an idea what to do now.



  • You can modify the frozen "_boot.py" to import a "my-frozen-main.py"



  • @yahia a)You could make boot.py and main.py make short and just start your own differently named scripts.
    b) If you build your own image anyhow, you could use machine.main() and add a statement into the file frozen/_boot.py to start your own main program, which however must not be called main.py. Unfortunately, this script must reside in the /flash file system of your device, like main.py. main.py and boot.py would be regenerated, but it's content either ignored (for main.py) or you leave it empty (boot.py). Your own main.py would simpy contain a statement like: "import mymagicsoftware", which may reside in frozen byecode.

    Boot.py and main.py are ordinary python scripts. The only thing that makes them special is, that they are created, if they do not exist, and automatically executed on boot.



Pycom on Twitter