How to disable all REPL

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
manseekingknowledge
Posts: 61
Joined: Sun Oct 29, 2017 5:14 pm

How to disable all REPL

Post by manseekingknowledge » Sat May 25, 2019 7:17 am

MicroPython v1.9.4-704-g9d9c2cf70

I want to completely disable REPL. WebREPL seems easy enough to prevent (just don't ever import webrepl_setup), but regardless of what I try REPL via a wired connection always works. Here are some of the things I've tried:

Code: Select all

# Attempt 1: Tried to disable terminal duplication by setting the stream to None per https://docs.micropython.org/en/latest/library/uos.html#terminal-redirection-and-duplication
uos.dupterm(None)

# Attempt 2:  Tried to disable terminal duplication by setting the stream to a UART object with the baudrate set to 0 instead of 115200
uos.dupterm(machine.UART(0, 0), 0)
uos.dupterm(machine.UART(1, 0), 1)

# Attempt 3: Tried to disable the UART pins by making them inputs
Pin(1, Pin.IN)
Pin(3, Pin.IN)

# Attempt 4: Tried to disable the UART pins by making them outputs
Pin(1, Pin.OUT, value=0)
Pin(3, Pin.OUT, value=0)
I realize that when my program starts that REPL will not be available, I'm just looking for an extra measure of security that will explicitly disable REPL in all cases, known and unknown. I don't need the UART interface for anything else, only programming the ESP8266.

What am I missing?

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

Re: How to disable all REPL

Post by jimmo » Sat May 25, 2019 2:03 pm

I think you're going to have to make a custom firmware build to disable it. If you follow through ports/esp8266/main.c, you can see how mp_reset() runs your boot.py and main.py then init_done() drops into the REPL loop.

manseekingknowledge
Posts: 61
Joined: Sun Oct 29, 2017 5:14 pm

Re: How to disable all REPL

Post by manseekingknowledge » Mon Jul 08, 2019 6:52 am

jimmo wrote:
Sat May 25, 2019 2:03 pm
I think you're going to have to make a custom firmware build to disable it. If you follow through ports/esp8266/main.c, you can see how mp_reset() runs your boot.py and main.py then init_done() drops into the REPL loop.
Based on this comment in mpconfigport.h I would think I could just set MICROPY_ENABLE_COMPILER to 0 to prevent REPL since all my modules are frozen, but it isn't working:

Code: Select all

// You can disable the built-in MicroPython compiler by setting the following
// config option to 0.  If you do this then you won't get a REPL prompt, but you
// will still be able to execute pre-compiled scripts, compiled with mpy-cross.
#define MICROPY_ENABLE_COMPILER     (1)

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

Re: How to disable all REPL

Post by jimmo » Mon Jul 08, 2019 7:05 am

I don't have the ESP8266 toolchain set up to test, but what's the error you get?

I'm guessing you might also need to disable other things that use the compiler, e.g.

Code: Select all

#define MICROPY_PY_BUILTINS_COMPILE (0)
#define MICROPY_PY_BUILTINS_EXECFILE (0)

manseekingknowledge
Posts: 61
Joined: Sun Oct 29, 2017 5:14 pm

Re: How to disable all REPL

Post by manseekingknowledge » Mon Jul 08, 2019 7:10 am

I'm up working too late. I came back to delete my comment, but your reply was too fast! ;)

I clicked on the minimal/mpconfigport.h, not esp8266/mpconfigport.h. There is no MICROPY_ENABLE_COMPILER in the esp8266 port.

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

Re: How to disable all REPL

Post by jimmo » Mon Jul 08, 2019 7:15 am

You can just add it to mpconfigport.h though?

Either way, I think that comment is a bit unclear, when it says you "won't get a REPL", it really means "won't get the ability to use a REPL in your firmware", because if you disable that flag, then pyexec_*_repl() will be unavailable. You will need to modify main.c to prevent it calling these functions.

manseekingknowledge
Posts: 61
Joined: Sun Oct 29, 2017 5:14 pm

Re: How to disable all REPL

Post by manseekingknowledge » Mon Jul 08, 2019 7:41 am

jimmo wrote:
Mon Jul 08, 2019 7:15 am
You can just add it to mpconfigport.h though?

Either way, I think that comment is a bit unclear, when it says you "won't get a REPL", it really means "won't get the ability to use a REPL in your firmware", because if you disable that flag, then pyexec_*_repl() will be unavailable. You will need to modify main.c to prevent it calling these functions.
Not sure I understand your first sentence. I was looking at the wrong copy of mpconfigport.h the first time, so no, I can't just change a build flag to prevent REPL.

As for your second sentence, completely disabling REPL is exactly what I want. I was try to avoid modifying MicroPython C code when I can, but I went ahead and commented out the REPL code in main.c as you suggested and based on my minimal testing thus far it seems to be doing what I want.

Code: Select all

    // Check if there are any dupterm objects registered and if not then
    // activate UART(0), or else there will never be any chance to get a REPL
//    size_t idx;
//    for (idx = 0; idx < MICROPY_PY_OS_DUPTERM; ++idx) {
//        if (MP_STATE_VM(dupterm_objs[idx]) != MP_OBJ_NULL) {
//            break;
//        }
//    }
//    if (idx == MICROPY_PY_OS_DUPTERM) {
//        mp_obj_t args[2];
//        args[0] = MP_OBJ_NEW_SMALL_INT(0);
//        args[1] = MP_OBJ_NEW_SMALL_INT(115200);
//        args[0] = pyb_uart_type.make_new(&pyb_uart_type, 2, 0, args);
//        args[1] = MP_OBJ_NEW_SMALL_INT(1);
//        extern mp_obj_t os_dupterm(size_t n_args, const mp_obj_t *args);
//        os_dupterm(2, args);
//        mp_hal_stdout_tx_str("Activated UART(0) for REPL\r\n");
//    }

kgschlosser
Posts: 22
Joined: Fri Apr 10, 2020 6:18 pm

Re: How to disable all REPL

Post by kgschlosser » Sun May 30, 2021 3:22 am

I know this is an old topic and I am looking to do this same thing on an ESP32. I am wondering if I placed the code below into boot.py would it stop the REPL from working? I have yet to test it but I believe that it will.

Code: Select all

import _thread

lock = _thread.allocate_lock()
lock.acquire()

def new_main_thread():
    # this would be where you want to run your code from
    # if you want to still use main.py you would have to import it here.
    
    # we want to hold up the main thread until this thread successfully starts
    # then release the main thread so it can lock it's self from ever doing anything else. 
    lock.release()
    import main
    
    
# start a new thread which is where you would run your program from.
_thread.start_new_thread(new_main_thread, ())

# by acquiring a thread lock 2 times it is going to cause the main thread to some to a halt. 
# using threads locks to stop  the main thread from continuing does not allow the REPL 
# to function properly and a person would not be able to input anything into it.
# 
# using thread locks is the only way that I know of on the ESP32 to pause a thread without 
# causing a spinning wheel that would consume the processor time. 
lock.acquire()
lock.acquire()


ph1lw21
Posts: 2
Joined: Sat May 08, 2021 8:54 pm

Re: How to disable all REPL

Post by ph1lw21 » Mon May 31, 2021 9:37 pm

To disable REPL could you not just set TXD0 to an input?

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: How to disable all REPL

Post by karfas » Tue Jun 01, 2021 3:44 pm

ph1lw21 wrote:
Mon May 31, 2021 9:37 pm
To disable REPL could you not just set TXD0 to an input?
Will most likely not work in all thinkable cases. When his program sets TXDO, it may happen that
a) someone connected serial might hit ^C before the output is set
b) the REPL might initialize communication, and might therefore set TXD0 as it likes

However, he could solder TXD0 to ground, remove the USB plug, ... :D
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

Post Reply