Skip to content

Commit 82eb13a

Browse files
committed
MassStorageCopy updated to manage several node names.
Depending of the board revision, mount point name could be different. Ex for: STM32F030R8 with a rev 1 node name is : "NUCLEO" while it is "NODE_F030R8" for rev c Ex: in boards.txt we could have: Nucleo_64.menu.Nucleo_64_board.NUCLEO_F030R8.node="NODE_F030R8,NUCLEO" Fix #6 Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
1 parent 1ba6db2 commit 82eb13a

File tree

5 files changed

+127
-42
lines changed

5 files changed

+127
-42
lines changed

linux/massStorageCopy

4.37 KB
Binary file not shown.

linux64/massStorageCopy

502 Bytes
Binary file not shown.

src/massStorageCopy/massStorageCopy.bat

-9
This file was deleted.

src/massStorageCopy/massStorageCopy.c

+88-24
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,64 @@
44
#include <stdlib.h>
55
#include <mntent.h>
66
#include <unistd.h>
7+
#include <errno.h>
8+
9+
static char *input_path = NULL;
10+
static char *output_path = NULL;
11+
static char *output_dev = NULL;
12+
static char **list_output_dev = NULL;
13+
static char *cmd = NULL;
14+
static FILE *aFile = NULL;
715

816
void usage(char *name)
917
{
10-
printf("Usage: %s [-I <filepath>] [-O <mountpoint> ]\n\n", name);
18+
printf("Usage: %s [-I <filepath>] [-O <mountpoint(s)> ]\n\n", name);
1119
printf("Mandatory options:\n");
1220
printf("\t-I: filepath binary to copy\n");
13-
printf("\t-O: mountpoint destination name\n");
21+
printf("\t-O: mountpoint(s) destination name.\n");
22+
printf("\t Could be a list (separated by','). Ex: \"NODE_1,NODE2,NODE_3\"\n");
23+
}
24+
25+
void free_ressource()
26+
{
27+
if(input_path)
28+
free(input_path);
29+
if(output_path)
30+
free(output_path);
31+
if(output_dev)
32+
free(output_dev);
33+
if(list_output_dev)
34+
free(list_output_dev);
35+
if(cmd)
36+
free(cmd);
37+
if(aFile)
38+
endmntent(aFile);
1439
}
1540

1641
int main(int argc, char *argv[])
1742
{
1843
int c, i;
1944
int ret = 0;
2045
int device_found = 0;
21-
char input_path[256] = "";
22-
char output_dev[256] = "";
23-
char output_path[256] = "";
24-
char cmd[512] = "";
2546
struct mntent *ent = NULL;
26-
FILE *aFile = NULL;
47+
char *p = NULL;
48+
int n_output_dev = 0;
49+
char scp_cmd[]="scp";
2750

2851
opterr = 0;
2952

3053
while ((c = getopt (argc, argv, "I:O:")) != -1)
3154
switch (c)
3255
{
3356
case 'I':
34-
strcpy(input_path, optarg);
57+
input_path = malloc(strlen(optarg)+1);
58+
if(input_path != NULL)
59+
strcpy(input_path, optarg);
3560
break;
3661
case 'O':
37-
strcpy(output_dev, optarg);
62+
output_dev = malloc(strlen(optarg)+1);
63+
if(output_dev != NULL)
64+
strcpy(output_dev, optarg);
3865
break;
3966
case '?':
4067
if ((optopt == 'I') || (optopt == 'O'))
@@ -46,47 +73,84 @@ int main(int argc, char *argv[])
4673
"Unknown option character `\\x%x'.\n",
4774
optopt);
4875
usage(argv[0]);
49-
return 1;
76+
free_ressource();
77+
return EINVAL;
5078
default:
5179
abort ();
5280
}
5381

82+
if((input_path == NULL) || (output_dev == NULL))
83+
{
84+
free_ressource();
85+
exit(ENOMEM);
86+
}
87+
5488
if (strlen(input_path) && strlen(output_dev))
5589
{
56-
//get the mounted devives list
90+
/* get the mounted devives list */
5791
aFile = setmntent("/proc/mounts", "r");
5892
if (aFile == NULL) {
5993
perror("setmntent");
60-
exit(1);
94+
free_ressource();
95+
exit(ENOENT);
6196
}
6297

63-
//now lets read the path of the device
64-
while (NULL != (ent = getmntent(aFile))) {
65-
if (strstr(ent->mnt_dir, output_dev)) {
66-
sprintf(output_path, "%s", ent->mnt_dir);
67-
device_found = 1;
68-
}
98+
p = strtok (output_dev, ",");
99+
100+
/* split output_dev and append tokens to list_output_dev */
101+
while (p) {
102+
list_output_dev = realloc (list_output_dev, sizeof (char*) * ++n_output_dev);
103+
104+
if (list_output_dev == NULL)
105+
exit (ENOMEM);
106+
107+
list_output_dev[n_output_dev-1] = p;
108+
109+
p = strtok (NULL, ",");
69110
}
70111

71-
endmntent(aFile);
112+
/* realloc one extra element for the last NULL */
113+
list_output_dev = realloc (list_output_dev, sizeof (char*) * (n_output_dev+1));
114+
list_output_dev[n_output_dev] = 0;
115+
116+
/* now lets read the path of the device */
117+
while ((NULL != (ent = getmntent(aFile))) && (!device_found)) {
118+
for (i = 0; (i < (n_output_dev)) && (!device_found); ++i) {
119+
if (strstr(ent->mnt_dir, list_output_dev[i])) {
120+
output_path = malloc(strlen(ent->mnt_dir)+1);
121+
if(output_path != NULL) {
122+
sprintf(output_path, "%s", ent->mnt_dir);
123+
} else {
124+
free_ressource();
125+
exit(ENOMEM);
126+
}
127+
device_found = 1;
128+
}
129+
}
130+
}
72131

73132
if(device_found) {
74133
printf("copying %s to %s\n", input_path, output_path);
75-
76-
sprintf(cmd, "scp %s %s", input_path, output_path);
77-
system(cmd);
134+
cmd = malloc(strlen(scp_cmd)+1+strlen(input_path)+1+strlen(output_path)+1);
135+
if(cmd != NULL) {
136+
sprintf(cmd, "%s %s %s", scp_cmd, input_path, output_path);
137+
} else {
138+
free_ressource();
139+
exit(ENOMEM);
140+
}
141+
ret = system(cmd);
78142

79143
} else {
80144
printf("%s not found. please ensure the device is correctly connected\n",
81145
output_dev);
82-
ret = -1;
146+
ret = ENODEV;
83147
}
84148
}
85149
else
86150
{
87151
printf("Missing argument\n");
88152
usage(argv[0]);
89153
}
90-
154+
free_ressource();
91155
return ret;
92156
}

win/massStorageCopy.bat

+39-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,39 @@
1-
@ECHO off
2-
SET SOURCE=%2
3-
SET SRC_PARSE=%SOURCE:/=\%
4-
SET TARGET=%4
5-
setlocal enabledelayedexpansion
6-
for /F "skip=1 tokens=*" %%a in ('WMIC LOGICALDISK where "volumename like '%TARGET%%%'" get deviceid') do if not defined id set id=%%a
7-
Call Set "deviceid=%%id: =%%"
8-
if not "%deviceid%" == "" (XCOPY %SRC_PARSE% %deviceid% /Y /Q >NUL
9-
echo Upload complete ) else ( echo %TARGET% not found. Please ensure the device is correctly connected)
1+
@ECHO off
2+
3+
REM Exit codes for xcopy
4+
REM code | Description
5+
REM 0 | Files were copied without error.
6+
REM 1 | No files were found to copy.
7+
REM 2 | The user pressed CTRL+C to terminate xcopy.
8+
REM 4 | Initialization error occurred. There is not enough memory or disk space, or you entered an invalid drive name or invalid syntax on the command line.
9+
REM 5 | Disk write error occurred.
10+
11+
SET SOURCE=%2
12+
SET SRC_PARSE=%SOURCE:/=\%
13+
SET TARGET=%4
14+
SET TARGET=%TARGET:\=%
15+
16+
call :parse %TARGET%
17+
echo %TARGET% not found. Please ensure the device is correctly connected.
18+
exit 7
19+
20+
:parse
21+
set list=%1
22+
set list=%list:"=%
23+
24+
for /f "tokens=1* delims=," %%a in ("%list%") DO (
25+
if not "%%a" == "" call :sub %%a
26+
if not "%%b" == "" call :parse "%%b"
27+
)
28+
goto :eof
29+
30+
31+
:sub
32+
setlocal enabledelayedexpansion
33+
for /F "skip=1 tokens=*" %%a in ('WMIC LOGICALDISK where "volumename like '%~1'" get deviceid 2^>NUL') do if not defined id set id=%%a
34+
call Set "deviceid=%%id: =%%"
35+
if not "%deviceid%" == "" (
36+
XCOPY %SRC_PARSE% %deviceid% /Y /Q
37+
if !errorlevel! == 0 (echo Upload complete on %1 ^(%deviceid%^))
38+
exit !errorlevel!)
39+
goto :eof

0 commit comments

Comments
 (0)