xref: /OK3568_Linux_fs/external/recovery/sdboot.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 "sdboot.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_SD(void)31 bool is_boot_from_SD(void)
32 {
33     bool bSDBoot = 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, "sdfwupdate");
44     if (s != NULL) {
45         bSDBoot = true;
46         LOGI(">>> Boot from SDcard\n");
47     } else {
48         bSDBoot = false;
49         LOGI(">>> Boot from non-SDcard\n");
50     }
51 
52     close(fd);
53     return bSDBoot;
54 }
55 
ensure_sd_mounted(bool * bSDMounted)56 void ensure_sd_mounted(bool *bSDMounted)
57 {
58     int i;
59     for (i = 0; i < 3; i++) {
60         if (0 == ensure_path_mounted(EX_SDCARD_ROOT)) {
61             *bSDMounted = true;
62             break;
63         } else {
64             LOGI("delay 1sec\n");
65             sleep(1);
66         }
67     }
68 
69     if (i == 3)
70         *bSDMounted = false;
71 }
72 
73 #define MaxLine 1024
get_cfg_Item(char * pFileName,char * pKey,char * pValue,int * pValueLen)74 static int get_cfg_Item(char *pFileName /*in*/, char *pKey /*in*/,
75                         char * pValue/*in out*/, int * pValueLen /*out*/)
76 {
77     int     ret = 0;
78     FILE    *fp = NULL;
79     char    *pTmp = NULL, *pEnd = NULL, *pBegin = NULL;
80 
81     char lineBuf[MaxLine];
82 
83     fp = fopen(pFileName, "r");
84     if (fp == NULL) {
85         ret = -1;
86         return ret;
87     }
88 
89     while (!feof(fp)) {
90         memset(lineBuf, 0, sizeof(lineBuf));
91         fgets(lineBuf, MaxLine, fp);
92         LOGI("lineBuf: %s ", lineBuf);
93 
94         pTmp = strchr(lineBuf, '=');
95         if (pTmp == NULL)
96             continue;
97 
98         pTmp = strstr(lineBuf, pKey);
99         if (pTmp == NULL)
100             continue;
101 
102         pTmp = pTmp + strlen(pKey);
103         pTmp = strchr(pTmp, '=');
104         if (pTmp == NULL)
105             continue;
106 
107         pTmp = pTmp + 1;
108 
109         while (1) {
110             if (*pTmp == ' ') {
111                 pTmp ++ ;
112             } else {
113                 pBegin = pTmp;
114                 if (*pBegin == '\n') {
115                     goto End;
116                 }
117                 break;
118             }
119         }
120 
121         while (1) {
122             if ((*pTmp == ' ' || *pTmp == '\n'))
123                 break;
124             else
125                 pTmp ++;
126         }
127         pEnd = pTmp;
128 
129         *pValueLen = pEnd - pBegin;
130         memcpy(pValue, pBegin, pEnd - pBegin);
131     }
132 
133 End:
134     if (fp == NULL)
135         fclose(fp);
136 
137     return 0;
138 }
139 
is_sdcard_update(void)140 bool is_sdcard_update(void)
141 {
142     int  ret = 0;
143     bool bSdMounted = false;
144     char configFile[64] = {0};
145     int vlen = 0;
146     char str_val[10] = {0};
147     char *str_key = "fw_update";
148 
149     LOGI("%s in\n", __func__);
150     ensure_sd_mounted(&bSdMounted);
151     if (!bSdMounted) {
152         LOGE("Error! SDcard not mounted\n");
153         return false;
154     }
155 
156     strlcpy(configFile, EX_SDCARD_ROOT, sizeof(configFile));
157     strlcat(configFile, "/sd_boot_config.config", sizeof(configFile));
158     LOGI("configFile = %s \n", configFile);
159     ret = get_cfg_Item(configFile, str_key, str_val, &vlen);
160 
161     if (ret != 0) {
162         LOGI("func get_cfg_Item err:%d \n", ret);
163         return false;
164     }
165 
166     LOGI("\n %s:%s \n", str_key, str_val);
167 
168     if (strcmp(str_val, "1") != 0) {
169         return false;
170     }
171 
172     LOGI("firmware update will from SDCARD. \n");
173     LOGI("%s out\n", __func__);
174     return true;
175 }
176