From 5a71001b1e2b396bcc8da4076a9efc63a5a117b8 Mon Sep 17 00:00:00 2001 From: Kebo Liu Date: Thu, 8 Feb 2024 05:31:23 +0800 Subject: [PATCH] [202311]Fix the sfputil treats page number as decimal instead of hexadecimal (#3153) * Fix the sfputil treats page number as decimal instead of hexadecimal (#22) Fix the sfputil treats page number as decimal instead of hexadecimal Signed-off-by: Kebo Liu * remove unreachable code Signed-off-by: Kebo Liu --------- Signed-off-by: Kebo Liu --- doc/Command-Reference.md | 4 ++-- sfputil/main.py | 42 +++++++++++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/doc/Command-Reference.md b/doc/Command-Reference.md index 13489eb7d3..80ae3da90d 100644 --- a/doc/Command-Reference.md +++ b/doc/Command-Reference.md @@ -13023,7 +13023,7 @@ Usage: sfputil read-eeprom [OPTIONS] Options: -p, --port Logical port name [required] - -n, --page EEPROM page number [required] + -n, --page EEPROM page number in hex [required] -o, --offset EEPROM offset within the page [required] -s, --size Size of byte to be read [required] --no-format Display non formatted data @@ -13055,7 +13055,7 @@ Usage: sfputil write-eeprom [OPTIONS] Options: -p, --port Logical port name [required] - -n, --page EEPROM page number [required] + -n, --page EEPROM page number in hex [required] -o, --offset EEPROM offset within the page [required] -d, --data Hex string EEPROM data [required] --wire-addr TEXT Wire address of sff8472 diff --git a/sfputil/main.py b/sfputil/main.py index 0e59fbe898..eddc43f3dc 100644 --- a/sfputil/main.py +++ b/sfputil/main.py @@ -697,16 +697,20 @@ def eeprom(port, dump_dom, namespace): # 'eeprom-hexdump' subcommand @show.command() @click.option('-p', '--port', metavar='', help="Display SFP EEPROM hexdump for port ") -@click.option('-n', '--page', metavar='', type=click.IntRange(0, MAX_EEPROM_PAGE), help="Display SFP EEEPROM hexdump for ") +@click.option('-n', '--page', metavar='', help="Display SFP EEEPROM hexdump for ") def eeprom_hexdump(port, page): """Display EEPROM hexdump of SFP transceiver(s)""" if port: if page is None: page = 0 - return_code, output = eeprom_hexdump_single_port(port, page) + else: + page = validate_eeprom_page(page) + return_code, output = eeprom_hexdump_single_port(port, int(str(page), base=16)) click.echo(output) sys.exit(return_code) else: + if page is not None: + page = validate_eeprom_page(page) logical_port_list = natsorted(platform_sfputil.logical) lines = [] for logical_port_name in logical_port_list: @@ -718,6 +722,23 @@ def eeprom_hexdump(port, page): lines.append(output) click.echo('\n'.join(lines)) +def validate_eeprom_page(page): + """ + Validate input page module EEPROM + Args: + page: str page input by user + Returns: + int page + """ + try: + page = int(str(page), base=16) + except ValueError: + click.echo('Please enter a numeric page number') + sys.exit(ERROR_NOT_IMPLEMENTED) + if page < 0 or page > MAX_EEPROM_PAGE: + click.echo(f'Error: Invalid page number {page}') + sys.exit(ERROR_INVALID_PAGE) + return page def eeprom_hexdump_single_port(logical_port_name, page): """ @@ -810,7 +831,7 @@ def eeprom_hexdump_pages_general(logical_port_name, pages, target_page): tuple(0, dump string) if success else tuple(error_code, error_message) """ if target_page is not None: - lines = [f'EEPROM hexdump for port {logical_port_name} page {target_page}h'] + lines = [f'EEPROM hexdump for port {logical_port_name} page {target_page:x}h'] else: lines = [f'EEPROM hexdump for port {logical_port_name}'] physical_port = logical_port_to_physical_port_index(logical_port_name) @@ -851,7 +872,7 @@ def eeprom_hexdump_pages_sff8472(logical_port_name, pages, target_page): tuple(0, dump string) if success else tuple(error_code, error_message) """ if target_page is not None: - lines = [f'EEPROM hexdump for port {logical_port_name} page {target_page}h'] + lines = [f'EEPROM hexdump for port {logical_port_name} page {target_page:x}h'] else: lines = [f'EEPROM hexdump for port {logical_port_name}'] physical_port = logical_port_to_physical_port_index(logical_port_name) @@ -1695,7 +1716,7 @@ def target(port_name, target): # 'read-eeprom' subcommand @cli.command() @click.option('-p', '--port', metavar='', help="Logical port name", required=True) -@click.option('-n', '--page', metavar='', type=click.IntRange(0, MAX_EEPROM_PAGE), help="EEPROM page number", required=True) +@click.option('-n', '--page', metavar='', help="EEPROM page number in hex", required=True) @click.option('-o', '--offset', metavar='', type=click.IntRange(0, MAX_EEPROM_OFFSET), help="EEPROM offset within the page", required=True) @click.option('-s', '--size', metavar='', type=click.IntRange(1, MAX_EEPROM_OFFSET + 1), help="Size of byte to be read", required=True) @click.option('--no-format', is_flag=True, help="Display non formatted data") @@ -1723,6 +1744,8 @@ def read_eeprom(port, page, offset, size, no_format, wire_addr): api = sfp.get_xcvr_api() if api is None: click.echo('Error: SFP EEPROM not detected!') + if page is not None: + page = validate_eeprom_page(page) if not isinstance(api, sff8472.Sff8472Api): overall_offset = get_overall_offset_general(api, page, offset, size) else: @@ -1743,7 +1766,7 @@ def read_eeprom(port, page, offset, size, no_format, wire_addr): # 'write-eeprom' subcommand @cli.command() @click.option('-p', '--port', metavar='', help="Logical port name", required=True) -@click.option('-n', '--page', metavar='', type=click.IntRange(0, MAX_EEPROM_PAGE), help="EEPROM page number", required=True) +@click.option('-n', '--page', metavar='', help="EEPROM page number in hex", required=True) @click.option('-o', '--offset', metavar='', type=click.IntRange(0, MAX_EEPROM_OFFSET), help="EEPROM offset within the page", required=True) @click.option('-d', '--data', metavar='', help="Hex string EEPROM data", required=True) @click.option('--wire-addr', help="Wire address of sff8472") @@ -1777,7 +1800,8 @@ def write_eeprom(port, page, offset, data, wire_addr, verify): if api is None: click.echo('Error: SFP EEPROM not detected!') sys.exit(EXIT_FAIL) - + if page is not None: + page = validate_eeprom_page(page) if not isinstance(api, sff8472.Sff8472Api): overall_offset = get_overall_offset_general(api, page, offset, len(bytes)) else: @@ -1813,11 +1837,11 @@ def get_overall_offset_general(api, page, offset, size): """ if api.is_flat_memory(): if page != 0: - raise ValueError(f'Invalid page number {page}, only page 0 is supported') + raise ValueError(f'Invalid page number {page:x}h, only page 0 is supported') if page != 0: if offset < MIN_OFFSET_FOR_NON_PAGE0: - raise ValueError(f'Invalid offset {offset} for page {page}, valid range: [128, 255]') + raise ValueError(f'Invalid offset {offset} for page {page:x}h, valid range: [80h, FFh]') if size + offset - 1 > MAX_EEPROM_OFFSET: raise ValueError(f'Invalid size {size}, valid range: [1, {255 - offset + 1}]')