You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2019/09/13 20:54:46 UTC
[GitHub] [mynewt-core] agross-korg commented on a change in pull request
#1993: OTP drivers and scripts
agross-korg commented on a change in pull request #1993: OTP drivers and scripts
URL: https://github.com/apache/mynewt-core/pull/1993#discussion_r324365012
##########
File path: hw/bsp/dialog_da1469x-dk-pro/otp_tool.py
##########
@@ -0,0 +1,538 @@
+#!/usr/bin/env python3
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+import serial
+import io
+import click
+import struct
+import binascii
+from typing import NamedTuple
+from enum import Enum
+import os
+
+
+class Cmd(object):
+ OTP_READ_KEY = 0
+ OTP_WRITE_KEY = 1
+ FLASH_READ = 2
+ FLASH_WRITE = 3
+ OTP_READ_CONFIG = 4
+ OTP_APPEND_VALUE = 5
+ OTP_INIT = 6
+
+
+class cmd_no_payload(NamedTuple):
+ som: int
+ cmd: int
+
+
+class cmd_read_key(NamedTuple):
+ som: int
+ cmd: int
+ segment: int
+ slot: int
+
+
+class cmd_write_key(NamedTuple):
+ som: int
+ cmd: int
+ segment: int
+ slot: int
+
+
+class cmd_append_value(NamedTuple):
+ som: int
+ cmd: int
+ length: int
+
+
+class cmd_response(NamedTuple):
+ som: int
+ cmd: int
+ status: int
+ length: int
+
+
+class cmd_flash_op(NamedTuple):
+ som: int
+ cmd: int
+ address: int
+ length: int
+
+
+def crc16(data: bytearray, offset, length):
+ if data is None or offset < 0:
+ return
+
+ if offset > len(data) - 1 and offset + length > len(data):
+ return 0
+
+ crc = 0
+ for i in range(length):
+ crc ^= data[offset + i] << 8
+
+ for j in range(8):
+ if (crc & 0x8000) > 0:
+ crc = (crc << 1) ^ 0x1021
+ else:
+ crc = crc << 1
+ return crc & 0xFFFF
+
+
+def validate_slot_index(ctx, param, value):
+ if value in range(8):
+ return value
+ else:
+ raise click.BadParameter("Slot value has to be between 0 and 7")
+
+
+@click.option('-i', '--index', required=True, type=int, help='key slot index',
+ callback=validate_slot_index)
+@click.option('-u', '--uart', required=True, help='uart port')
+@click.option('-s', '--segment', type=click.Choice(['signature', 'data',
+ 'qspi']), help='OTP segment', required=True)
+@click.command(help='Read a single key from OTP key segment')
+def otp_read_key(index, segment, uart):
+ seg_map = {'signature': 0, 'data': 1, 'qspi': 2}
+
+ try:
+ ser = serial.Serial(port=uart, baudrate=115200, timeout=1,
+ bytesize=8, stopbits=serial.STOPBITS_ONE)
+ except serial.SerialException:
+ raise SystemExit("Failed to open serial port")
+
+ cmd = cmd_read_key(0xaa55aa55, Cmd.OTP_READ_KEY, seg_map[segment], index)
+ data = struct.pack('IIII', *cmd)
+
+ try:
+ ser.write(data)
+ except serial.SerialException:
+ raise SystemExit("Failed to write to %s" % uart)
+
+ data = ser.read(16)
+ if len(data) != 16:
+ raise SystemExit("Failed to receive response, exiting")
+
+ response = cmd_response._make(struct.unpack_from('IIII', data))
+ if response.status == 0:
+ key = ser.read(32)
+ print("key:" + key.hex())
+ else:
+ raise SystemExit("Error reading key with status %s" %
+ hex(response.status))
+
+
+@click.argument('infile')
+@click.option('-u', '--uart', required=True, help='uart port')
+@click.option('-i', '--index', required=True, type=int, help='key slot index',
+ callback=validate_slot_index)
+@click.option('-s', '--segment', type=click.Choice(['signature', 'data',
+ 'qspi']), help='OTP segment', required=True,)
+@click.command(help='Write a single key to OTP key segment')
+def otp_write_key(infile, index, segment, uart):
+
+ key = bytearray()
+ try:
+ with open(infile, "rb") as f:
+ buf = f.read(32)
+ key = struct.unpack('IIIIIIII', buf)
+ except IOError:
Review comment:
good point, let me handle that case.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services