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