-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrktools.c
232 lines (214 loc) · 5.87 KB
/
rktools.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* Copyright (C) 2023 Rockchip Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include "rktools.h"
#include "common.h"
/**
* 从/proc/cmdline 获取串口的节点
*
*/
char *getSerial()
{
char *ans = (char*)malloc(20);
char param[1024];
int fd, ret;
char *s = NULL;
fd = open("/proc/cmdline", O_RDONLY);
ret = read(fd, (char*)param, 1024);
LOGI("cmdline=%s\n", param);
s = strstr(param, "console");
if (s == NULL) {
LOGI("no found console in cmdline\n");
free(ans);
ans = NULL;
return ans;
} else {
s = strstr(s, "=");
if (s == NULL) {
free(ans);
ans = NULL;
return ans;
}
strcpy(ans, "/dev/");
char *str = ans + 5;
s++;
while (*s != ' ') {
*str = *s;
str++;
s++;
}
*str = '\0';
LOGI("read console from cmdline is %s\n", ans);
}
return ans;
}
/**
* 设置flash 节点
*/
static char result_point[4][20] = {'\0'}; //0-->emmc, 1-->sdcard, 2-->SDIO, 3-->SDcombo
int readFile(DIR* dir, char* filename)
{
char name[30] = {'\0'};
int i;
strcpy(name, filename);
strcat(name, "/type");
int fd = openat(dirfd(dir), name, O_RDONLY);
if (fd == -1) {
LOGE("Error: openat %s error %s.\n", name, strerror(errno));
return -1;
}
char resultBuf[10] = {'\0'};
if (read(fd, resultBuf, sizeof(resultBuf)) < 1) {
return -1;
}
for (i = 0; i < strlen(resultBuf); i++) {
if (resultBuf[i] == '\n') {
resultBuf[i] = '\0';
break;
}
}
for (i = 0; i < 4; i++) {
if (strcmp(typeName[i], resultBuf) == 0) {
//printf("type is %s.\n", typeName[i]);
return i;
}
}
LOGE("Error:no found type!\n");
return -1;
}
void init_sd_emmc_point()
{
DIR* dir = opendir("/sys/bus/mmc/devices/");
if (dir != NULL) {
struct dirent* de;
while ((de = readdir(dir))) {
if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0 )
continue;
//if (de->d_type == 4) //dir
// printf("dir name : %s \n", de->d_name);
if (strncmp(de->d_name, "mmc", 3) == 0) {
//printf("find mmc is %s.\n", de->d_name);
char flag = de->d_name[3];
int ret = -1;
ret = readFile(dir, de->d_name);
if (ret != -1) {
strcpy(result_point[ret], point_items[flag - '0']);
} else {
strcpy(result_point[ret], "");
}
}
}
}
closedir(dir);
}
static void wait_for_device(const char* fn)
{
int tries = 0;
int ret;
struct stat buf;
do {
++tries;
ret = stat(fn, &buf);
if (ret) {
LOGI("stat %s try %d: %s\n", fn, tries, strerror(errno));
sleep(1);
}
} while (ret && tries < 10);
if (ret) {
LOGI("failed to stat %s\n", fn);
}
}
void setFlashPoint()
{
if (!isMtdDevice())
wait_for_device(MISC_PARTITION_NAME_BLOCK);
init_sd_emmc_point();
setenv(EMMC_POINT_NAME, result_point[MMC], 1);
//SDcard 有两个挂载点
if (access(result_point[SD], F_OK) == 0)
setenv(SD_POINT_NAME_2, result_point[SD], 1);
char name_t[22];
if (strlen(result_point[SD]) > 0) {
strcpy(name_t, result_point[SD]);
strcat(name_t, "p1");
}
if (access(name_t, F_OK) == 0)
setenv(SD_POINT_NAME, name_t, 1);
LOGI("emmc_point is %s\n", getenv(EMMC_POINT_NAME));
LOGI("sd_point is %s\n", getenv(SD_POINT_NAME));
LOGI("sd_point_2 is %s\n", getenv(SD_POINT_NAME_2));
}
#define MTD_PATH "/proc/mtd"
//判断是MTD还是block 设备
bool isMtdDevice()
{
char param[2048];
int fd, ret;
char *s = NULL;
fd = open("/proc/cmdline", O_RDONLY);
ret = read(fd, (char*)param, 2048);
close(fd);
s = strstr(param, "storagemedia");
if (s == NULL) {
LOGI("no found storagemedia in cmdline, default is not MTD.\n");
return false;
} else {
s = strstr(s, "=");
if (s == NULL) {
LOGI("no found storagemedia in cmdline, default is not MTD.\n");
return false;
}
s++;
while (*s == ' ') {
s++;
}
if (strncmp(s, "mtd", 3) == 0 ) {
LOGI("Now is MTD.\n");
return true;
} else if (strncmp(s, "sd", 2) == 0) {
LOGI("Now is SD.\n");
if ( !access(MTD_PATH, F_OK) ) {
fd = open(MTD_PATH, O_RDONLY);
ret = read(fd, (char*)param, 2048);
close(fd);
s = strstr(param, "mtd");
if (s == NULL) {
LOGI("no found mtd.\n");
return false;
}
LOGI("Now is MTD.\n");
return true;
}
}
}
LOGI("devices is not MTD.\n");
return false;
}