2 * SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2018 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
6 #include <helper/log.h>
9 #include "jtag_usb_common.h"
11 static char *jtag_usb_location;
14 * 2 * 7 chars: max 7 ports
15 * 1 char: test for overflow
19 #define JTAG_USB_MAX_LOCATION_LENGTH 16
21 void jtag_usb_set_location(const char *location)
23 if (strnlen(location, JTAG_USB_MAX_LOCATION_LENGTH) ==
24 JTAG_USB_MAX_LOCATION_LENGTH)
25 LOG_WARNING("usb location string is too long!!\n");
27 free(jtag_usb_location);
29 jtag_usb_location = strndup(location, JTAG_USB_MAX_LOCATION_LENGTH);
32 const char *jtag_usb_get_location(void)
34 return jtag_usb_location;
37 bool jtag_usb_location_equal(uint8_t dev_bus, uint8_t *port_path,
40 size_t path_step, string_length;
44 /* strtok need non const char */
45 loc = strndup(jtag_usb_get_location(), JTAG_USB_MAX_LOCATION_LENGTH);
46 string_length = strnlen(loc, JTAG_USB_MAX_LOCATION_LENGTH);
48 ptr = strtok(loc, "-");
50 LOG_WARNING("no '-' in usb path\n");
54 string_length -= strnlen(ptr, string_length);
55 /* check bus mismatch */
56 if (atoi(ptr) != dev_bus)
60 while (path_step < path_len) {
61 ptr = strtok(NULL, ".");
63 /* no more tokens in path */
67 /* path mismatch at some step */
68 if (path_step < path_len && atoi(ptr) != port_path[path_step])
72 string_length -= strnlen(ptr, string_length) + 1;
75 /* walked the full path, all elements match */
76 if (path_step == path_len && !string_length)