hc
2025-02-14 bbb9540dc49f70f6b703d1c8d1b85fa5f602d86e
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
/**
 * Copyright (C) 2018 Fuzhou Rockchip Electronics Co., Ltd
 * author: Chad.ma <Chad.ma@rock-chips.com>
 *
 * 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 "sdboot.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_SD(void){
    bool bSDBoot = false;
    char param[1024];
    int fd, ret;
    char *s=NULL;
    printf("read cmdline\n");
    memset(param,0,1024);
 
    fd = open("/proc/cmdline", O_RDONLY);
    ret = read(fd, (char*)param, 1024);
 
    s = strstr(param,"sdfwupdate");
    if(s != NULL){
        bSDBoot = true;
        printf(">>> Boot from SDcard\n");
    }else{
        bSDBoot = false;
        printf(">>> Boot from non-SDcard\n");
    }
 
    close(fd);
    return bSDBoot;
}
 
void ensure_sd_mounted(bool *bSDMounted) {
    int i;
    for(i = 0; i < 3; i++) {
        if(0 == ensure_path_mounted(EX_SDCARD_ROOT)){
            *bSDMounted = true;
            break;
        }else {
            printf("delay 1sec\n");
            sleep(1);
        }
    }
 
    if (i == 3)
        *bSDMounted = false;
}
 
#define MaxLine 1024
 
//»ñÈ¡ÅäÖÃÏî
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);
        printf("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_sdcard_update(void) {
    int  ret = 0;
    bool bSdMounted = false;
    char configFile[64] = {0};
    int vlen = 0;
    char str_val[10] = {0};
    char *str_key = "fw_update";
 
    printf("%s in\n",__func__);
    ensure_sd_mounted(&bSdMounted);
    if (!bSdMounted) {
        printf("Error! SDcard not mounted\n");
        return false;
    }
 
    strlcpy(configFile, EX_SDCARD_ROOT, sizeof(configFile));
    strlcat(configFile, "/sd_boot_config.config", sizeof(configFile));
    printf("configFile = %s \n", configFile);
    ret = get_cfg_Item(configFile, str_key, str_val, &vlen);
 
    if(ret != 0) {
        printf("func get_cfg_Item err:%d \n", ret);
        return false;
    }
 
    printf("\n %s:%s \n", str_key, str_val);
 
    if(strcmp(str_val, "1") != 0) {
        return false;
    }
 
    printf("firmware update will from SDCARD. \n");
    printf("%s out\n",__func__);
    return true;
}