{
enum st_cmds cmd;
const char* devname;
+ char *serial;
const char* filename;
stm32_addr_t addr;
size_t size;
static void usage(void)
{
- puts("stlinkv1 command line: ./st-flash [--debug] [--reset] {read|write} /dev/sgX path addr <size>");
+ puts("stlinkv1 command line: ./st-flash [--debug] [--reset] [--serial <iSerial>] {read|write} /dev/sgX path addr <size>");
puts("stlinkv1 command line: ./st-flash [--debug] /dev/sgX erase");
- puts("stlinkv2 command line: ./st-flash [--debug] [--reset] {read|write} path addr <size>");
- puts("stlinkv2 command line: ./st-flash [--debug] erase");
- puts(" use hex format for addr and <size>");
+ puts("stlinkv2 command line: ./st-flash [--debug] [--reset] [--serial <iSerial>] {read|write} path addr <size>");
+ puts("stlinkv2 command line: ./st-flash [--debug] [--serial <iSerial>] erase");
+ puts(" use hex format for addr, <iSerial> and <size>");
}
static int get_opts(struct opts* o, int ac, char** av)
o->reset = 0;
}
+ if (strcmp(av[0], "--serial") == 0)
+ {
+ ac--;
+ av++;
+ int i=strlen(av[0]);
+ if(i%2 != 0){
+ puts("no valid hex value, length must be multiple of 2\n");
+ return -1;
+ }
+ int j=0;
+ while(i>=0 && j<=13){
+ char buffer[3]={0};
+ memcpy(buffer,&av[0][i],2);
+ o->serial[12-j] = (char)strtol((const char*)buffer,NULL, 16);
+ j++;
+ i-=2;
+ }
+ ac--;
+ av++;
+ }
+ else
+ {
+ o->serial = NULL;
+ }
+
if (ac < 1) return -1;
/* stlinkv2 */
{
stlink_t* sl = NULL;
struct opts o;
+ char serial_buffer[13] = {0};
+ o.serial = serial_buffer;
int err = -1;
o.size = 0;
}
else /* stlinkv2 */
{
- sl = stlink_open_usb(o.log_level, 1);
+ sl = stlink_open_usb(o.log_level, 1, o.serial);
if (sl == NULL) goto on_error;
sl->verbose = o.log_level;
}
parse_options(argc, argv, &state);
switch (state.stlink_version) {
case 2:
- sl = stlink_open_usb(state.logging_level, 0);
+ sl = stlink_open_usb(state.logging_level, 0, NULL);
if(sl == NULL) return 1;
break;
case 1:
/* try version 1 then version 2 */
gui->sl = stlink_v1_open(0, 1);
if (gui->sl == NULL) {
- gui->sl = stlink_open_usb(0, 1);
+ gui->sl = stlink_open_usb(0, 1, NULL);
}
if (gui->sl == NULL) {
stlink_gui_set_info_error_message (gui, "Failed to connect to STLink."); return;
stlink_t* sl;
sl = stlink_v1_open(0, 1);
if (sl == NULL)
- sl = stlink_open_usb(0, 1);
+ sl = stlink_open_usb(0, 1, NULL);
return sl;
}
sig_init();
- sl = stlink_open_usb(10, 1);
+ sl = stlink_open_usb(10, 1, NULL);
if (sl != NULL) {
printf("ST-Linky proof-of-concept terminal :: Created by Necromant for lulz\n");
stlink_version(sl);
};
-stlink_t* stlink_open_usb(const int verbose, int reset) {
+stlink_t* stlink_open_usb(const int verbose, int reset, char *p_usb_iserial) {
stlink_t* sl = NULL;
struct stlink_libusb* slu = NULL;
int error = -1;
sl->backend_data = slu;
sl->core_stat = STLINK_CORE_STAT_UNKNOWN;
-
if (libusb_init(&(slu->libusb_ctx))) {
WLOG("failed to init libusb context, wrong version of libraries?\n");
goto on_error;
if (desc.idVendor!=USB_ST_VID) continue;
if (devBus && devAddr)
if ((libusb_get_bus_number(list[cnt])!=devBus) || (libusb_get_device_address(list[cnt])!=devAddr)) continue;
- if ( (desc.idProduct == USB_STLINK_32L_PID) || (desc.idProduct == USB_STLINK_NUCLEO_PID) ) break;
+ if ( (desc.idProduct == USB_STLINK_32L_PID) || (desc.idProduct == USB_STLINK_NUCLEO_PID) ){
+ if ((p_usb_iserial != NULL)){
+ unsigned char buffer[13];
+ struct libusb_device_handle* handle;
+ libusb_open(list[cnt], &handle);
+ libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, buffer, 13);
+ libusb_close(handle);
+ if (memcmp(p_usb_iserial,&buffer,12) == 0){
+ break;
+ }else{
+ continue;
+ }
+ }else{
+ break;
+ }
+ }
if (desc.idProduct == USB_STLINK_PID) {
slu->protocoll = 1;
break;
unsigned int cmd_len;
};
- stlink_t* stlink_open_usb(const int verbose, int reset);
+ stlink_t* stlink_open_usb(const int verbose, int reset, char *p_usb_iserial);
#ifdef __cplusplus
ac = ac;
av = av;
- sl = stlink_open_usb(10, 1);
+ sl = stlink_open_usb(10, 1, NULL);
if (sl != NULL) {
printf("-- version\n");
stlink_version(sl);