Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read EFuse from CMake (IDFGH-8890) #10311

Closed
tsctrl opened this issue Dec 5, 2022 · 1 comment
Closed

Read EFuse from CMake (IDFGH-8890) #10311

tsctrl opened this issue Dec 5, 2022 · 1 comment
Assignees
Labels
Resolution: Done Issue is done internally Status: Done Issue is done internally Type: Feature Request Feature request for IDF

Comments

@tsctrl
Copy link

tsctrl commented Dec 5, 2022

Is your feature request related to a problem?

i am frustrated when i am accidentally flasing my encrypted and brick my device, adding capability in build cmake system could help us to read efuses value to stop the build and avoid from reflashing the encrypted device.

Describe the solution you'd like.

adding capability in build cmake system could help us to read efuses value to stop the build and avoid from reflashing the encrypted device

Describe alternatives you've considered.

please let me know other approach thats interact to cmake, i dont want to modify espressif idf.py script to have this capability and reading the files in bash is not reliable and impacted by sdk version update.

Additional context.

  • edit: this feature could be beneficial for any other solution or issue that require efuses values from build time
@tsctrl tsctrl added the Type: Feature Request Feature request for IDF label Dec 5, 2022
@espressif-bot espressif-bot added the Status: Opened Issue is new label Dec 5, 2022
@github-actions github-actions bot changed the title Read EFuse from CMake Read EFuse from CMake (IDFGH-8890) Dec 5, 2022
@espressif-bot espressif-bot added Status: Selected for Development Issue is selected for development and removed Status: Opened Issue is new labels Dec 7, 2022
@KonstantinKondrashov
Copy link
Collaborator

Hi @tsctrl!
Yes, it is possible to add this feature to the build cmake system. The CMake can run espefuse.py summary --format json to get the state of efuses (if need to save it into a file then add --file efuse_file.json). I created some functions to extract what will be needed.

The efuse json representation looks like this:

{
    "FLASH_CRYPT_CNT": {
        "bit_len": 7,
        "block": 0,
        "category": "security",
        "description": "Flash encryption mode counter",
        "efuse_type": "uint:7",
        "name": "FLASH_CRYPT_CNT",
        "pos": 20,
        "readable": true,
        "value": 0,
        "word": 0,
        "writeable": true
    },
//....
}
espefuse_get_json_summary(efuse_json) # connects to chip once and read out all efuses to a string var

espefuse_get_efuse(efuse_value ${efuse_json} "FLASH_CRYPT_CNT" "value") # extract required efuses and properties one by one
message("FLASH_CRYPT_CNT:" ${efuse_value})

espefuse_get_efuse(efuse_value ${efuse_json} "ABS_DONE_1" "value")
message("ABS_DONE_1:" ${efuse_value})

espefuse_get_efuse(efuse_value ${efuse_json} "MAC" "value")
message("MAC:" ${efuse_value})

It returns the same format of values as shown in espefuse.py summary.

FLASH_CRYPT_CNT:0
ABS_DONE_1:OFF
MAC:94:b9:7e:5a:6e:58 (CRC 0xe2 OK)

You will be able to use these APIs in the esp-idf/examples/get-started/hello_world/CMakeLists.txt file.

If it is urgent, you can use this patch. Create the esp-idf/components/efuse/project_include.cmake file and put it there.

# reads efuses from chip using "espefuse.py summary" and returns a cleaned json string
function(espefuse_get_json_summary result)
    execute_process(COMMAND ${ESPEFUSEPY} summary
        --format json
        OUTPUT_VARIABLE info
        RESULT_VARIABLE exit_code
        OUTPUT_STRIP_TRAILING_WHITESPACE)
    if(NOT ${exit_code} EQUAL 0 AND NOT ${exit_code} EQUAL 1)
        message(WARNING "espefuse.py execution failed (${result}), problem with ... (see above)")
    endif()
    string(FIND ${info} "=== Run \"summary\" command ===" output_variable)
    MATH(EXPR output_variable "${output_variable} + 29")
    string(SUBSTRING ${info} ${output_variable} -1 output)
    set(${result} ${output} PARENT_SCOPE)
endfunction()

# It takes the efuse json string and returns a value of property for a given efuse name 
function(espefuse_get_efuse result efuse_json efuse_name efuse_property)
    string(JSON cur_efuse GET ${efuse_json} ${efuse_name})
    string(JSON ret_value GET ${cur_efuse} ${efuse_property})
    set(${result} ${ret_value} PARENT_SCOPE)
endfunction()

@espressif-bot espressif-bot added Status: In Progress Work is in progress Status: Reviewing Issue is being reviewed and removed Status: Selected for Development Issue is selected for development Status: In Progress Work is in progress labels Dec 13, 2022
@espressif-bot espressif-bot added Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally Resolution: Done Issue is done internally and removed Status: Reviewing Issue is being reviewed Resolution: NA Issue resolution is unavailable labels Jan 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Done Issue is done internally Status: Done Issue is done internally Type: Feature Request Feature request for IDF
Projects
None yet
Development

No branches or pull requests

3 participants