1 /*
2 * Copyright (C) 2023 Rockchip Electronics Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <stdbool.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <pthread.h>
26 #include <sys/types.h>
27 #include <dirent.h>
28 #include <stdio.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include "rktools.h"
34 #include "common.h"
35
36 /**
37 * 从/proc/cmdline 获取串口的节点
38 *
39 */
getSerial()40 char *getSerial()
41 {
42 char *ans = (char*)malloc(20);
43 char param[1024];
44 int fd, ret;
45 char *s = NULL;
46 fd = open("/proc/cmdline", O_RDONLY);
47 ret = read(fd, (char*)param, 1024);
48 LOGI("cmdline=%s\n", param);
49 s = strstr(param, "console");
50 if (s == NULL) {
51 LOGI("no found console in cmdline\n");
52 free(ans);
53 ans = NULL;
54 return ans;
55 } else {
56 s = strstr(s, "=");
57 if (s == NULL) {
58 free(ans);
59 ans = NULL;
60 return ans;
61 }
62
63 strcpy(ans, "/dev/");
64 char *str = ans + 5;
65 s++;
66 while (*s != ' ') {
67 *str = *s;
68 str++;
69 s++;
70 }
71 *str = '\0';
72 LOGI("read console from cmdline is %s\n", ans);
73 }
74
75 return ans;
76 }
77
78 /**
79 * 设置flash 节点
80 */
81 static char result_point[4][20] = {'\0'}; //0-->emmc, 1-->sdcard, 2-->SDIO, 3-->SDcombo
readFile(DIR * dir,char * filename)82 int readFile(DIR* dir, char* filename)
83 {
84 char name[30] = {'\0'};
85 int i;
86
87 strcpy(name, filename);
88 strcat(name, "/type");
89 int fd = openat(dirfd(dir), name, O_RDONLY);
90 if (fd == -1) {
91 LOGE("Error: openat %s error %s.\n", name, strerror(errno));
92 return -1;
93 }
94 char resultBuf[10] = {'\0'};
95 if (read(fd, resultBuf, sizeof(resultBuf)) < 1) {
96 return -1;
97 }
98 for (i = 0; i < strlen(resultBuf); i++) {
99 if (resultBuf[i] == '\n') {
100 resultBuf[i] = '\0';
101 break;
102 }
103 }
104 for (i = 0; i < 4; i++) {
105 if (strcmp(typeName[i], resultBuf) == 0) {
106 //printf("type is %s.\n", typeName[i]);
107 return i;
108 }
109 }
110
111 LOGE("Error:no found type!\n");
112 return -1;
113 }
114
init_sd_emmc_point()115 void init_sd_emmc_point()
116 {
117 DIR* dir = opendir("/sys/bus/mmc/devices/");
118 if (dir != NULL) {
119 struct dirent* de;
120 while ((de = readdir(dir))) {
121 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0 )
122 continue;
123 //if (de->d_type == 4) //dir
124 // printf("dir name : %s \n", de->d_name);
125
126 if (strncmp(de->d_name, "mmc", 3) == 0) {
127 //printf("find mmc is %s.\n", de->d_name);
128 char flag = de->d_name[3];
129 int ret = -1;
130 ret = readFile(dir, de->d_name);
131 if (ret != -1) {
132 strcpy(result_point[ret], point_items[flag - '0']);
133 } else {
134 strcpy(result_point[ret], "");
135 }
136 }
137 }
138 }
139 closedir(dir);
140 }
141
wait_for_device(const char * fn)142 static void wait_for_device(const char* fn)
143 {
144 int tries = 0;
145 int ret;
146 struct stat buf;
147 do {
148 ++tries;
149 ret = stat(fn, &buf);
150 if (ret) {
151 LOGI("stat %s try %d: %s\n", fn, tries, strerror(errno));
152 sleep(1);
153 }
154 } while (ret && tries < 10);
155 if (ret) {
156 LOGI("failed to stat %s\n", fn);
157 }
158 }
159
setFlashPoint()160 void setFlashPoint()
161 {
162 if (!isMtdDevice())
163 wait_for_device(MISC_PARTITION_NAME_BLOCK);
164
165 init_sd_emmc_point();
166 setenv(EMMC_POINT_NAME, result_point[MMC], 1);
167 //SDcard 有两个挂载点
168
169 if (access(result_point[SD], F_OK) == 0)
170 setenv(SD_POINT_NAME_2, result_point[SD], 1);
171 char name_t[22];
172 if (strlen(result_point[SD]) > 0) {
173 strcpy(name_t, result_point[SD]);
174 strcat(name_t, "p1");
175 }
176 if (access(name_t, F_OK) == 0)
177 setenv(SD_POINT_NAME, name_t, 1);
178
179 LOGI("emmc_point is %s\n", getenv(EMMC_POINT_NAME));
180 LOGI("sd_point is %s\n", getenv(SD_POINT_NAME));
181 LOGI("sd_point_2 is %s\n", getenv(SD_POINT_NAME_2));
182 }
183
184 #define MTD_PATH "/proc/mtd"
185 //判断是MTD还是block 设备
isMtdDevice()186 bool isMtdDevice()
187 {
188 char param[2048];
189 int fd, ret;
190 char *s = NULL;
191 fd = open("/proc/cmdline", O_RDONLY);
192 ret = read(fd, (char*)param, 2048);
193 close(fd);
194 s = strstr(param, "storagemedia");
195 if (s == NULL) {
196 LOGI("no found storagemedia in cmdline, default is not MTD.\n");
197 return false;
198 } else {
199 s = strstr(s, "=");
200 if (s == NULL) {
201 LOGI("no found storagemedia in cmdline, default is not MTD.\n");
202 return false;
203 }
204
205 s++;
206 while (*s == ' ') {
207 s++;
208 }
209
210 if (strncmp(s, "mtd", 3) == 0 ) {
211 LOGI("Now is MTD.\n");
212 return true;
213 } else if (strncmp(s, "sd", 2) == 0) {
214 LOGI("Now is SD.\n");
215 if ( !access(MTD_PATH, F_OK) ) {
216 fd = open(MTD_PATH, O_RDONLY);
217 ret = read(fd, (char*)param, 2048);
218 close(fd);
219
220 s = strstr(param, "mtd");
221 if (s == NULL) {
222 LOGI("no found mtd.\n");
223 return false;
224 }
225 LOGI("Now is MTD.\n");
226 return true;
227 }
228 }
229 }
230 LOGI("devices is not MTD.\n");
231 return false;
232 }
233