-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusbboot.c
executable file
·190 lines (160 loc) · 4.62 KB
/
usbboot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* Copyright (C) 2018 Fuzhou Rockchip Electronics Co., Ltd
* author: Chad.ma <[email protected]>
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "roots.h"
#include "usbboot.h"
#include "common.h"
extern size_t strlcpy(char *dst, const char *src, size_t dsize);
extern size_t strlcat(char *dst, const char *src, size_t dsize);
bool is_boot_from_udisk(void)
{
bool bUDisk = false;
char param[1024];
int fd, ret;
char *s = NULL;
LOGI("read cmdline\n");
memset(param, 0, 1024);
fd = open("/proc/cmdline", O_RDONLY);
ret = read(fd, (char*)param, 1024);
s = strstr(param, "usbfwupdate");
if (s != NULL) {
bUDisk = true;
LOGI(">>> Boot from U-Disk\n");
} else {
bUDisk = false;
LOGI(">>> Boot from non-U-Disk\n");
}
close(fd);
return bUDisk;
}
void ensure_udisk_mounted(bool *bMounted)
{
int i;
bool bSucc = false;
for (i = 0; i < 3; i++) {
if (0 == ensure_path_mounted(EX_UDISK_ROOT)) {
*bMounted = true;
bSucc = true;
break;
} else {
LOGI("delay 1 sec to try /mnt/udisk\n");
sleep(1);
}
}
if (!bSucc) { // try another mount point
for (i = 0; i < 3; i++) {
if (0 == ensure_path_mounted(EX_UDISK_ROOT2)) {
*bMounted = true;
bSucc = true;
break;
} else {
LOGI("delay 1 sec to try /mnt/usb_storage\n");
sleep(1);
}
}
}
if (bSucc)
*bMounted = false;
}
#define MaxLine 1024
static int get_cfg_Item(char *pFileName /*in*/, char *pKey /*in*/,
char * pValue/*in out*/, int * pValueLen /*out*/)
{
int ret = 0;
FILE *fp = NULL;
char *pTmp = NULL, *pEnd = NULL, *pBegin = NULL;
char lineBuf[MaxLine];
fp = fopen(pFileName, "r");
if (fp == NULL) {
ret = -1;
return ret;
}
while (!feof(fp)) {
memset(lineBuf, 0, sizeof(lineBuf));
fgets(lineBuf, MaxLine, fp);
LOGI("lineBuf: %s ", lineBuf);
pTmp = strchr(lineBuf, '=');
if (pTmp == NULL)
continue;
pTmp = strstr(lineBuf, pKey);
if (pTmp == NULL)
continue;
pTmp = pTmp + strlen(pKey);
pTmp = strchr(pTmp, '=');
if (pTmp == NULL)
continue;
pTmp = pTmp + 1;
while (1) {
if (*pTmp == ' ') {
pTmp ++ ;
} else {
pBegin = pTmp;
if (*pBegin == '\n') {
goto End;
}
break;
}
}
while (1) {
if ((*pTmp == ' ' || *pTmp == '\n'))
break;
else
pTmp ++;
}
pEnd = pTmp;
*pValueLen = pEnd - pBegin;
memcpy(pValue, pBegin, pEnd - pBegin);
}
End:
if (fp == NULL)
fclose(fp);
return 0;
}
bool is_udisk_update(void)
{
int ret = 0;
bool bUdiskMounted = false;
char configFile[64] = {0};
int vlen = 0;
char str_val[10] = {0};
char *str_key = "fw_update";
LOGI("%s in\n", __func__);
ensure_udisk_mounted(&bUdiskMounted);
if (!bUdiskMounted) {
LOGI("Error! U-Disk not mounted\n");
return false;
}
strlcpy(configFile, EX_UDISK_ROOT, sizeof(configFile));
strlcat(configFile, "/sd_boot_config.config", sizeof(configFile));
LOGI("configFile = %s \n", configFile);
ret = get_cfg_Item(configFile, str_key, str_val, &vlen);
if (ret != 0) {
LOGI("func get_cfg_Item err:%d \n", ret);
return false;
}
LOGI("\n %s:%s \n", str_key, str_val);
if (strcmp(str_val, "1") != 0) {
return false;
}
LOGI("firmware update will from UDisk.\n");
LOGI("%s out\n", __func__);
return true;
}