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

nvs_tool.py extracts strings wrongly in json format (IDFGH-14504) #15274

Open
3 tasks done
taliesin opened this issue Jan 23, 2025 · 0 comments
Open
3 tasks done

nvs_tool.py extracts strings wrongly in json format (IDFGH-14504) #15274

taliesin opened this issue Jan 23, 2025 · 0 comments
Labels
Status: Opened Issue is new

Comments

@taliesin
Copy link

taliesin commented Jan 23, 2025

Answers checklist.

  • I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
  • I have updated my IDF branch (master or release) to the latest version and checked that the issue is present there.
  • I have searched the issue tracker for a similar issue and not found a similar issue.

General issue report

I created a CSV file containing some factory data:

key,type,encoding,value
factory_data,namespace,,
hw_version,data,string,"0.15.2"
production_date,data,u64,1737662604
laserdiode_beta,data,u16,400

and used
python3 /components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py generate factory_data_0.15.2.csv factory_data.bin 0x4000
to create a NVS binary image

Then used
python3 /components/nvs_flash/nvs_partition_tool/nvs_tool.py factory_data.bin -f text

which says (emtpy stuff not shown):

Page no. 0, Status: Full, Version: 2, CRC32: b9ba2d84, Page address: 0x0
 Entry state bitmap: aa fe ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 
 000. Written, Namespace Index: 000, Type: uint8_t   , Span: 001, Chunk Index: 255, CRC32: a5762796 | factory_data : 1
 001. Written, Namespace Index: 001, Type: string    , Span: 002, Chunk Index: 255, CRC32: bee86f42 | hw_version : Size=7, CRC32=ee2d98d5
      0x000  30 2e 31 35 2e 32 00 ff ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  0.15.2.ÿÿÿÿÿÿÿÿÿ ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
 003. Written, Namespace Index: 001, Type: uint64_t  , Span: 001, Chunk Index: 255, CRC32: cdf2ce5a | production_date : 1737662604
 004. Written, Namespace Index: 001, Type: uint16_t  , Span: 001, Chunk Index: 255, CRC32:  c5e5c90 | laserdiode_beta : 400
 005. Empty

Then I tried using the JSON output, which I piped to jq to filter stuff:

python3 nvs_tool.py -f json factory_data.bin | jq '.pages[].entries[]|select(.key == "hw_version")'

which returns:

{
  "raw": "ASEC/0Jv6L5od192ZXJzaW9uAAAAAAAABwD//9WYLe4=",
  "state": "Written",
  "is_empty": false,
  "index": 1,
  "metadata": {
    "namespace": 1,
    "type": "string",
    "span": 2,
    "chunk_index": 255,
    "crc": {
      "original": 3202903874,
      "computed": 3202903874,
      "data_original": 3995965653,
      "data_computed": 3995965653
    }
  },
  "children": [
    {
      "raw": "MC4xNS4yAP////////////////////////////////8=",
      "state": "Written",
      "is_empty": false,
      "index": 2,
      "metadata": {
        "namespace": 48,
        "type": "0x2e",
        "span": 49,
        "chunk_index": 53,
        "crc": {
          "original": 4278202926,
          "computed": 463762067,
          "data_original": 4294967295,
          "data_computed": 0
        }
      },
      "children": [],
      "key": null,
      "data": null
    }
  ],
  "key": "hw_version",
  "data": {
    "value": [
      7,
      3995965653
    ],
    "size": 7,
    "crc": 3995965653
  }
}

Obviously the data.value entry is wrong. I checked the nvs_parser.py code to find:

# Deal with variable length entries
if nvs_const.item_type[i_type] in ['string', 'blob_data', 'blob']:
    size = int.from_bytes(data[:2], byteorder='little')
    crc = int.from_bytes(data[4:8], byteorder='little')
    return {'value': [size, crc], 'size': size, 'crc': crc}

... which is exactly what I get, but that is obviously wrong!

@espressif-bot espressif-bot added the Status: Opened Issue is new label Jan 23, 2025
@github-actions github-actions bot changed the title nvs_tool.py extracts strings wrongly in json format nvs_tool.py extracts strings wrongly in json format (IDFGH-14504) Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Opened Issue is new
Projects
None yet
Development

No branches or pull requests

2 participants