hc
2024-08-12 233ab1bd4c5697f5cdec94e60206e8c6ac609b4c
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
/*
 * Copyright (c) 2017 Spreadtrum
 *
 * WCN partition parser for different CPU have different path with EMMC and NAND
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 <linux/delay.h>
#include <linux/dirent.h>
#include <linux/fcntl.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/fsnotify.h>
#include <linux/fs_struct.h>
#include <linux/module.h>
#include <linux/path.h>
#include <linux/stat.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/statfs.h>
#include <linux/string.h>
#include <linux/types.h>
#include <linux/unistd.h>
#include <linux/uaccess.h>
#include <linux/version.h>
#include <linux/vmalloc.h>
#include <linux/version.h>
 
#include "mdbg_type.h"
#include "wcn_parn_parser.h"
 
#define ROOT_PATH "/"
#define ETC_PATH "/etc"
#define VENDOR_ETC_PATH "/etc"
#define ETC_FSTAB "/etc/fstab"
#define FSTAB_PATH_NUM 3
#define CONF_COMMENT '#'
#define CONF_LF '\n'
#define CONF_DELIMITERS " =\n\r\t"
#define CONF_VALUES_DELIMITERS "=\n\r\t"
#define CONF_MAX_LINE_LEN 255
static const char *prefix = "fstab.s";
static char fstab_name[128];
static char fstab_dir[FSTAB_PATH_NUM][32] = {
           ROOT_PATH, ETC_PATH, VENDOR_ETC_PATH};
 
static char *fgets(char *buf, int buf_len, struct file *fp)
{
   int ret;
   int i = 0;
 
#if KERNEL_VERSION(4, 14, 0) <= LINUX_VERSION_CODE
   ret = kernel_read(fp, (void *)buf, buf_len, &fp->f_pos);
#else
   ret = kernel_read(fp, fp->f_pos, buf, buf_len);
#endif
 
   if (ret <= 0)
       return NULL;
 
   while (buf[i++] != '\n' && i < ret)
       ;
 
   if (i <= ret)
       fp->f_pos += i;
   else
       return NULL;
 
   if (i < buf_len)
       buf[i] = 0;
 
   return buf;
}
 
 
 
static int load_fstab_conf(const char *p_path, char *WCN_PATH)
{
   struct file *p_file;
   char *p_name;
   char line[CONF_MAX_LINE_LEN+1];
   char *p;
   char *temp;
   bool match_flag;
 
   match_flag = false;
   p = line;
   WCN_INFO("Attempt to load conf from %s\n", p_path);
 
   p_file = filp_open(p_path, O_RDONLY, 0);
   if (IS_ERR(p_file)) {
       WCN_ERR("open file %s error not find\n",
           p_path);
       return PTR_ERR(p_file);
   }
 
   /* read line by line */
   while (fgets(line, CONF_MAX_LINE_LEN+1, p_file) != NULL) {
 
       if ((line[0] == CONF_COMMENT) || (line[0] == CONF_LF))
           continue;
 
       p = line;
       p_name = strsep(&p, CONF_DELIMITERS);
       if (p_name != NULL) {
           temp = strstr(p_name, "userdata");
           if (temp != NULL) {
               snprintf(WCN_PATH, strlen(p_name)+1,
                   "%s", p_name);
               WCN_PATH[strlen(WCN_PATH) - strlen(temp)]
                   = '\0';
               snprintf(WCN_PATH, strlen(WCN_PATH)+9,
                   "%s%s", WCN_PATH, "wcnmodem");
               match_flag = true;
               break;
           }
       }
   }
 
   filp_close(p_file, NULL);
   if (match_flag)
       return 0;
   else
       return -1;
}
 
static int prefixcmp(const char *str, const char *prefix)
{
   for (; ; str++, prefix++)
       if (!*prefix)
           return 0;
       else if (*str != *prefix)
           return (unsigned char)*prefix - (unsigned char)*str;
}
 
#if KERNEL_VERSION(3, 19, 0) <= LINUX_VERSION_CODE
static int find_callback(struct dir_context *ctx, const char *name, int namlen,
            loff_t offset, u64 ino, unsigned int d_type)
#else
static int find_callback(void *ctx, const char *name, int namlen,
            loff_t offset, u64 ino, unsigned int d_type)
#endif
{
   int tmp;
 
   tmp = prefixcmp(name, prefix);
   if (tmp == 0) {
       if (sizeof(fstab_name) > strlen(fstab_name) + strlen(name) + 2)
           strcat(fstab_name, name);
       WCN_INFO("full fstab name %s\n", fstab_name);
   }
 
   return 0;
}
 
static struct dir_context ctx =  {
   .actor = find_callback,
};
 
int parse_firmware_path(char *firmware_path)
{
   u32 ret = -1;
   u32 loop;
   struct file *file1;
 
   WCN_INFO("%s entry\n", __func__);
   for (loop = 0; loop < FSTAB_PATH_NUM; loop++) {
       file1 = NULL;
       WCN_DEBUG("dir:%s: loop:%d\n", fstab_dir[loop], loop);
       file1 = filp_open(fstab_dir[loop], O_DIRECTORY, 0);
       if (IS_ERR(file1)) {
           WCN_ERR("%s open error:%d\n",
               fstab_dir[loop], (int)IS_ERR(file1));
           continue;
       }
       memset(fstab_name, 0, sizeof(fstab_name));
       strncpy(fstab_name, fstab_dir[loop], sizeof(fstab_name));
       if (strlen(fstab_name) > 1)
           fstab_name[strlen(fstab_name)] = '/';
       iterate_dir(file1, &ctx);
       fput(file1);
       ret = load_fstab_conf(fstab_name, firmware_path);
       WCN_INFO("%s:load conf ret %d\n", fstab_dir[loop], ret);
       if (!ret) {
           WCN_INFO("[%s]:%s\n", fstab_name, firmware_path);
           return 0;
       }
   }
   /* for yunos */
   ret = load_fstab_conf(ETC_FSTAB, firmware_path);
   if (!ret) {
       WCN_INFO(ETC_FSTAB":%s !\n", firmware_path);
       return 0;
   }
 
   return ret;
}