Skip to content

Commit

Permalink
Add serial number support
Browse files Browse the repository at this point in the history
-lsusb can list device serial number. If it is empty, try 'sudo'.
-ms serial[*] will monitor this device.

Not all imx ROM support it. please use uuu -lsusb to check it.

Signed-off-by: Frank Li <Frank.Li@nxp.com>
  • Loading branch information
nxpfrankli committed Feb 14, 2024
1 parent 38405f3 commit 33f8086
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 14 deletions.
3 changes: 2 additions & 1 deletion libuuu/libuuu.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ int uuu_for_each_cfg(uuu_show_cfg fn, void *p);
typedef int(*uuu_ls_file)(const char *path, void *p);
int uuu_for_each_ls_file(uuu_ls_file fn, const char *path, void *p);

typedef int(*uuu_ls_usb_devices)(const char *path, const char *chip, const char *pro, uint16_t vid, uint16_t pid, uint16_t bcd, void *p);
typedef int(*uuu_ls_usb_devices)(const char *path, const char *chip, const char *pro, uint16_t vid, uint16_t pid, uint16_t bcd, const char *serial_no, void *p);
int uuu_for_each_devices(uuu_ls_usb_devices fn, void *p);

int uuu_run_cmd(const char * cmd, int dry);
Expand All @@ -126,6 +126,7 @@ int uuu_run_cmd_script(const char *script, int dry);
int uuu_auto_detect_file(const char * filename);
int uuu_wait_uuu_finish(int deamon, int dry);
int uuu_add_usbpath_filter(const char *path);
int uuu_add_usbserial_no_filter(const char *serial_no);

/*Set timeout wait for known devices appeared*/
int uuu_set_wait_timeout(int timeout_in_seconds);
Expand Down
8 changes: 4 additions & 4 deletions libuuu/rominfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ static constexpr std::array<ROM_INFO, 16> g_RomInfo
ROM_INFO{ "MX6UL", 0x00910000, ROM_INFO_HID | ROM_INFO_HID_MX6 | ROM_INFO_HID_SKIP_DCD },
ROM_INFO{ "MX6ULL", 0x00910000, ROM_INFO_HID | ROM_INFO_HID_MX6 | ROM_INFO_HID_SKIP_DCD },
ROM_INFO{ "MX6SLL", 0x00910000, ROM_INFO_HID | ROM_INFO_HID_MX6 | ROM_INFO_HID_SKIP_DCD },
ROM_INFO{ "MX8MQ", 0x00910000, ROM_INFO_HID | ROM_INFO_HID_MX6 | ROM_INFO_HID_SKIP_DCD | ROM_INFO_NEED_BAREBOX_FULL_IMAGE},
ROM_INFO{ "MX8MQ", 0x00910000, ROM_INFO_HID | ROM_INFO_HID_MX6 | ROM_INFO_HID_SKIP_DCD | ROM_INFO_NEED_BAREBOX_FULL_IMAGE, 4},
ROM_INFO{ "MX7ULP", 0x2f018000, ROM_INFO_HID | ROM_INFO_HID_MX6 | ROM_INFO_HID_SKIP_DCD },
ROM_INFO{ "MXRT106X", 0x1000, ROM_INFO_HID | ROM_INFO_HID_MX6 | ROM_INFO_HID_SKIP_DCD },
ROM_INFO{ "MX8QXP", 0x0, ROM_INFO_HID | ROM_INFO_HID_NO_CMD | ROM_INFO_HID_UID_STRING },
ROM_INFO{ "MX8QXP", 0x0, ROM_INFO_HID | ROM_INFO_HID_NO_CMD | ROM_INFO_HID_UID_STRING, 4},
ROM_INFO{ "MX28", 0x0, ROM_INFO_HID},
ROM_INFO{ "MX815", 0x0, ROM_INFO_HID | ROM_INFO_HID_NO_CMD | ROM_INFO_HID_UID_STRING | ROM_INFO_HID_EP1 | ROM_INFO_HID_PACK_SIZE_1020 | ROM_INFO_HID_ROMAPI},
ROM_INFO{ "MX95", 0x0, ROM_INFO_HID | ROM_INFO_HID_NO_CMD | ROM_INFO_HID_UID_STRING | ROM_INFO_HID_EP1 | ROM_INFO_HID_PACK_SIZE_1020 },
ROM_INFO{ "MX815", 0x0, ROM_INFO_HID | ROM_INFO_HID_NO_CMD | ROM_INFO_HID_UID_STRING | ROM_INFO_HID_EP1 | ROM_INFO_HID_PACK_SIZE_1020 | ROM_INFO_HID_ROMAPI, 4},
ROM_INFO{ "MX95", 0x0, ROM_INFO_HID | ROM_INFO_HID_NO_CMD | ROM_INFO_HID_UID_STRING | ROM_INFO_HID_EP1 | ROM_INFO_HID_PACK_SIZE_1020, 4},
ROM_INFO{ "SPL", 0x0, ROM_INFO_HID | ROM_INFO_HID_MX6 | ROM_INFO_SPL_JUMP | ROM_INFO_HID_SDP_NO_MAX_PER_TRANS},
ROM_INFO{ "SPL1", 0x0, ROM_INFO_HID | ROM_INFO_HID_MX6 | ROM_INFO_SPL_JUMP | ROM_INFO_HID_SDP_NO_MAX_PER_TRANS | ROM_INFO_AUTO_SCAN_UBOOT_POS},
};
Expand Down
1 change: 1 addition & 0 deletions libuuu/rominfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ struct ROM_INFO
const char * m_name;
uint32_t free_addr;
uint32_t flags;
int serial_idx;
};

const ROM_INFO * search_rom_info(const std::string &s);
Expand Down
87 changes: 85 additions & 2 deletions libuuu/usbhotplug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@
#include "cmd.h"
#include "libcomm.h"
#include "libuuu.h"
#include "rominfo.h"
#include "vector"
#include <time.h>
#include <iostream>

using chrono::milliseconds;
using chrono::operator ""ms;
Expand Down Expand Up @@ -134,7 +136,7 @@ class CAutoList
int m_rc = 0;
};

static struct {
struct filter {
vector<string> list;
mutex lock;

Expand All @@ -143,7 +145,9 @@ static struct {
lock_guard<mutex> guard{lock};
list.emplace_back(std::move(filter));
}
};

static struct: public filter {
bool is_valid(const string& path)
{
lock_guard<mutex> guard{lock};
Expand All @@ -156,6 +160,26 @@ static struct {
}
} g_filter_usbpath;


static struct: public filter {
bool is_valid(const string& serial_no)
{
lock_guard<mutex> guard{lock};
if (list.empty())
return true;

if (serial_no.empty())
return false;

for(auto it: list) {
if (compare_str(serial_no.substr(0, it.length()), it, true))
return true;
}

return false;
}
} g_filter_usbserial_no;

struct Timer
{
using Clock = chrono::steady_clock;
Expand Down Expand Up @@ -212,6 +236,47 @@ static string get_device_path(libusb_device *dev)
return str;
}

#define SERIAL_NO_MAX 512

static string get_device_serial_no(libusb_device *dev, struct libusb_device_descriptor *desc, ConfigItem *item)
{
string serial;
struct libusb_device_handle *dev_handle = NULL;
int sid = desc->iSerialNumber;
int ret;

if (!sid) {
const ROM_INFO *info= search_rom_info(item);
sid = info->serial_idx;
}

serial.resize(SERIAL_NO_MAX);
libusb_open(dev, &dev_handle);
if (sid && dev_handle)
ret = libusb_get_string_descriptor_ascii(dev_handle, sid, (unsigned char*)serial.c_str(), SERIAL_NO_MAX);
libusb_close(dev_handle);
if(ret >= 0)
serial.resize(ret);

return serial;
}

static string get_device_serial_no(libusb_device *dev)
{
string str;

struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0) {
set_last_err_string("failure get device descriptor");
return str;
}

ConfigItem *item = get_config()->find(desc.idVendor, desc.idProduct, desc.bcdDevice);

return get_device_serial_no(dev, &desc, item);
}

static int open_libusb(libusb_device *dev, void **usb_device_handle)
{
int retry = 10;
Expand Down Expand Up @@ -267,6 +332,8 @@ static int run_usb_cmds(ConfigItem *item, libusb_device *dev, short bcddevice)

string str;
str = get_device_path(dev);
str += "-";
str += get_device_serial_no(dev);
nt.str = (char*)str.c_str();
call_notify(nt);

Expand Down Expand Up @@ -311,6 +378,10 @@ static int usb_add(libusb_device *dev)

if (item)
{
string serial = get_device_serial_no(dev, &desc, item);
if (!g_filter_usbserial_no.is_valid(serial))
return -1;

g_known_device_state = KnownDeviceToDo;

/*
Expand Down Expand Up @@ -475,6 +546,11 @@ int CmdUsbCtx::look_for_match_device(const char *pro)
continue;

ConfigItem *item = get_config()->find(desc.idVendor, desc.idProduct, desc.bcdDevice);

string serial_no = get_device_serial_no(dev, &desc, item);
if (!g_filter_usbserial_no.is_valid(serial_no))
continue;

if (item && item->m_protocol == str_to_upper(pro))
{
uuu_notify nt;
Expand Down Expand Up @@ -513,6 +589,12 @@ int uuu_add_usbpath_filter(const char *path)
return 0;
}

int uuu_add_usbserial_no_filter(const char *serial_no)
{
g_filter_usbserial_no.push_back(serial_no);
return 0;
}

int uuu_for_each_devices(uuu_ls_usb_devices fn, void *p)
{
CAutoList l;
Expand All @@ -536,7 +618,8 @@ int uuu_for_each_devices(uuu_ls_usb_devices fn, void *p)
ConfigItem *item = get_config()->find(desc.idVendor, desc.idProduct, desc.bcdDevice);
if (item)
{
if (fn(str.c_str(), item->m_chip.c_str(), item->m_protocol.c_str(), desc.idVendor, desc.idProduct, desc.bcdDevice, p))
string serial = get_device_serial_no(dev, &desc, item);
if (fn(str.c_str(), item->m_chip.c_str(), item->m_protocol.c_str(), desc.idVendor, desc.idProduct, desc.bcdDevice, serial.c_str(), p))
{
set_last_err_string("call back return error");
return -1;
Expand Down
29 changes: 22 additions & 7 deletions uuu/uuu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ char g_sample_cmd_list[] = {
};

vector<string> g_usb_path_filter;
vector<string> g_usb_serial_no_filter;

int g_verbose = 0;
static bool g_start_usb_transfer;
Expand Down Expand Up @@ -189,6 +190,7 @@ void print_help(bool detail = false)
" -no-bmap Ignore .bmap files even if flash commands specify them\n"
" -m USBPATH Only monitor these paths.\n"
" -m 1:2 -m 1:3\n\n"
" -ms serial_no Monitor the serial number prefix of the device using 'serial_no'.\n"
" -t Timeout second for wait known usb device appeared\n"
" -T Timeout second for wait next known usb device appeared at stage switch\n"
" -e set environment variable key=value\n"
Expand Down Expand Up @@ -502,7 +504,7 @@ class ShowNotify
{
string str;
str = m_dev;
str.resize(8, ' ');
str.resize(12, ' ');

string_ex s;
s.format("%2d/%2d", m_cmd_index+1, m_cmd_total);
Expand All @@ -514,7 +516,7 @@ class ShowNotify
{
int width = get_console_width();
int info, bar;
info = 14;
info = 18;
bar = 40;

if (m_IsEmptyLine)
Expand Down Expand Up @@ -666,6 +668,13 @@ int progress(uuu_notify nt, void *p)
str += g_usb_path_filter[i] + " ";
}

if (!g_usb_serial_no_filter.empty())
{
str += " at serial_no ";
for (auto it: g_usb_serial_no_filter)
str += it + "*";
}

print_oneline(str);
print_oneline("");
if ((*np)[nt.id].m_dev == "Prep" && !g_start_usb_transfer)
Expand Down Expand Up @@ -824,17 +833,17 @@ void print_udev()
fprintf(stderr, "\tsudo udevadm control --reload\n");
}

int print_usb_device(const char *path, const char *chip, const char *pro, uint16_t vid, uint16_t pid, uint16_t bcd, void * /*p*/)
int print_usb_device(const char *path, const char *chip, const char *pro, uint16_t vid, uint16_t pid, uint16_t bcd, const char *serial_no, void * /*p*/)
{
printf("\t%s\t %s\t %s\t 0x%04X\t0x%04X\t 0x%04X\n", path, chip, pro, vid, pid, bcd);
printf("\t%s\t %s\t %s\t 0x%04X\t0x%04X\t 0x%04X\t %s\n", path, chip, pro, vid, pid, bcd, serial_no);
return 0;
}

void print_lsusb()
{
cout << "Connected Known USB Devices\n";
printf("\tPath\t Chip\t Pro\t Vid\t Pid\t BcdVersion\n");
printf("\t==================================================\n");
printf("\tPath\t Chip\t Pro\t Vid\t Pid\t BcdVersion\t Serial_no\n");
printf("\t====================================================================\n");

uuu_for_each_devices(print_usb_device, NULL);
}
Expand Down Expand Up @@ -979,6 +988,12 @@ int main(int argc, char **argv)
uuu_add_usbpath_filter(argv[i]);
g_usb_path_filter.push_back(argv[i]);
}
else if (s == "-ms")
{
i++;
uuu_add_usbserial_no_filter(argv[i]);
g_usb_serial_no_filter.push_back(argv[i]);
}
else if (s == "-t")
{
i++;
Expand Down Expand Up @@ -1126,7 +1141,7 @@ int main(int argc, char **argv)
if (g_verbose)
{
printf("%sBuild in config:%s\n", g_vt_boldwhite, g_vt_default);
printf("\tPctl\t Chip\t\t Vid\t Pid\t BcdVersion\n");
printf("\tPctl\t Chip\t\t Vid\t Pid\t BcdVersion\t Serial_No\n");
printf("\t==================================================\n");
uuu_for_each_cfg(print_cfg, NULL);

Expand Down

0 comments on commit 33f8086

Please sign in to comment.