File: file.c

package info (click to toggle)
skiboot 5.3.3-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 13,280 kB
  • ctags: 13,840
  • sloc: ansic: 77,199; asm: 1,002; sh: 997; cpp: 894; tcl: 408; makefile: 325; python: 166; pascal: 65
file content (351 lines) | stat: -rw-r--r-- 7,816 bytes parent folder | download
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* Copyright 2013-2014 IBM Corp.
 *
 * 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.
 */
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>

#include <ccan/container_of/container_of.h>

#include <mtd/mtd-abi.h>

#include "libflash.h"
#include "blocklevel.h"

struct file_data {
	int fd;
	char *name;
	char *path;
	struct blocklevel_device bl;
};

static int file_release(struct blocklevel_device *bl)
{
	struct file_data *file_data = container_of(bl, struct file_data, bl);
	close(file_data->fd);
	file_data->fd = -1;
	return 0;
}

static int file_reacquire(struct blocklevel_device *bl)
{
	struct file_data *file_data = container_of(bl, struct file_data, bl);
	int fd;

	fd = open(file_data->path, O_RDWR);
	if (fd == -1)
		return FLASH_ERR_PARM_ERROR;
	file_data->fd = fd;
	return 0;
}

static int file_read(struct blocklevel_device *bl, uint32_t pos, void *buf, uint32_t len)
{
	struct file_data *file_data = container_of(bl, struct file_data, bl);
	int rc, count = 0;

	rc = lseek(file_data->fd, pos, SEEK_SET);
	/* errno should remain set */
	if (rc != pos)
		return FLASH_ERR_PARM_ERROR;

	while (count < len) {
		rc = read(file_data->fd, buf, len);
		/* errno should remain set */
		if (rc == -1 || rc == 0)
			return FLASH_ERR_BAD_READ;

		count += rc;
	}

	return 0;
}

static int file_write(struct blocklevel_device *bl, uint32_t dst, const void *src,
		uint32_t len)
{
	struct file_data *file_data = container_of(bl, struct file_data, bl);
	int rc, count = 0;

	rc = lseek(file_data->fd, dst, SEEK_SET);
	/* errno should remain set */
	if (rc != dst)
		return FLASH_ERR_PARM_ERROR;

	while (count < len) {
		rc = write(file_data->fd, src, len);
		/* errno should remain set */
		if (rc == -1)
			return FLASH_ERR_VERIFY_FAILURE;

		count += rc;
	}

	return 0;
}

/*
 * Due to to the fact these interfaces are ultimately supposed to deal with
 * flash, an erase function must be implemented even when the flash images
 * are backed by regular files.
 * Also, erasing flash leaves all the bits set to 1. This may be expected
 * by higher level functions so this function should also emulate that
 */
static int file_erase(struct blocklevel_device *bl, uint32_t dst, uint32_t len)
{
	unsigned long long int d = ULLONG_MAX;
	int i = 0;
	int rc;

	while (len - i > 0) {
		rc = file_write(bl, dst + i, &d, len - i > sizeof(d) ? sizeof(d) : len - i);
		if (rc)
			return rc;
		i += sizeof(d);
	}

	return 0;
}

static int mtd_erase(struct blocklevel_device *bl, uint32_t dst, uint32_t len)
{
	struct file_data *file_data = container_of(bl, struct file_data, bl);
	struct erase_info_user erase_info = {
		.start = dst,
		.length = len
	};

	if (ioctl(file_data->fd, MEMERASE, &erase_info) == -1)
		return FLASH_ERR_PARM_ERROR;

	return 0;
}

static int get_info_name(struct file_data *file_data, char **name)
{
	char *path, *lpath;
	int len;
	struct stat st;

	if (asprintf(&path, "/proc/self/fd/%d", file_data->fd) == -1)
		return FLASH_ERR_MALLOC_FAILED;

	if (lstat(path, &st)) {
		free(path);
		return FLASH_ERR_PARM_ERROR;
	}

	lpath = malloc(st.st_size + 1);
	if (!lpath) {
		free(path);
		return FLASH_ERR_MALLOC_FAILED;
	}

	len = readlink(path, lpath, st.st_size +1);
	if (len == -1) {
		free(path);
		free(lpath);
		return FLASH_ERR_PARM_ERROR;
	}
	lpath[len] = '\0';

	*name = lpath;

	free(path);
	return 0;
}


static int mtd_get_info(struct blocklevel_device *bl, const char **name,
		uint32_t *total_size, uint32_t *erase_granule)
{
	struct file_data *file_data = container_of(bl, struct file_data, bl);
	struct mtd_info_user mtd_info;
	int rc;

	rc = ioctl(file_data->fd, MEMGETINFO, &mtd_info);
	if (rc == -1)
		return FLASH_ERR_BAD_READ;

	if (total_size)
		*total_size = mtd_info.size;

	if (erase_granule)
		*erase_granule = mtd_info.erasesize;

	if (name) {
		rc = get_info_name(file_data, &(file_data->name));
		if (rc)
			return rc;
		*name = file_data->name;
	}

	return 0;
}

static int file_get_info(struct blocklevel_device *bl, const char **name,
		uint32_t *total_size, uint32_t *erase_granule)
{
	struct file_data *file_data = container_of(bl, struct file_data, bl);
	struct stat st;
	int rc;

	if (fstat(file_data->fd, &st))
		return FLASH_ERR_PARM_ERROR;

	if (total_size)
		*total_size = st.st_size;

	if (erase_granule)
		*erase_granule = 1;

	if (name) {
		rc = get_info_name(file_data, &(file_data->name));
		if (rc)
			return rc;
		*name = file_data->name;
	}

	return 0;
}

int file_init(int fd, struct blocklevel_device **bl)
{
	struct file_data *file_data;
	struct stat sbuf;

	if (!bl)
		return FLASH_ERR_PARM_ERROR;

	*bl = NULL;

	file_data = calloc(1, sizeof(struct file_data));
	if (!file_data)
		return FLASH_ERR_MALLOC_FAILED;

	file_data->fd = fd;
	file_data->bl.reacquire = &file_reacquire;
	file_data->bl.release = &file_release;
	file_data->bl.read = &file_read;
	file_data->bl.write = &file_write;
	file_data->bl.erase = &file_erase;
	file_data->bl.get_info = &file_get_info;
	file_data->bl.erase_mask = 0;

	/*
	 * If the blocklevel_device is only inited with file_init() then keep
	 * alive is assumed, as fd will change otherwise and this may break
	 * callers assumptions.
	 */
	file_data->bl.keep_alive = 1;

	/*
	 * Unfortunately not all file descriptors are created equal...
	 * Here we check to see if the file descriptor is to an MTD device, in
	 * which case we have to erase and get the size of it differently.
	 */
	if (fstat(file_data->fd, &sbuf) == -1)
		goto out;

	/* Won't be able to handle other than MTD devices for now */
	if (S_ISCHR(sbuf.st_mode)) {
		file_data->bl.erase = &mtd_erase;
		file_data->bl.get_info = &mtd_get_info;
		file_data->bl.flags = WRITE_NEED_ERASE;
		mtd_get_info(&file_data->bl, NULL, NULL, &(file_data->bl.erase_mask));
		file_data->bl.erase_mask--;
	} else if (!S_ISREG(sbuf.st_mode)) {
		/* If not a char device or a regular file something went wrong */
		goto out;
	}

	*bl = &(file_data->bl);
	return 0;
out:
	free(file_data);
	return FLASH_ERR_PARM_ERROR;
}

int file_init_path(const char *path, int *r_fd, bool keep_alive,
		struct blocklevel_device **bl)
{
	int fd, rc;
	char *path_ptr = NULL;
	struct file_data *file_data;

	if (!path || !bl)
		return FLASH_ERR_PARM_ERROR;

	fd = open(path, O_RDWR);
	if (fd == -1)
		return FLASH_ERR_PARM_ERROR;

	/*
	 * strdup() first so don't have to deal with malloc failure after
	 * file_init()
	 */
	path_ptr = strdup(path);
	if (!path_ptr) {
		rc = FLASH_ERR_MALLOC_FAILED;
		goto out;
	}

	rc = file_init(fd, bl);
	if (rc)
		goto out;

	file_data = container_of(*bl, struct file_data, bl);
	file_data->bl.keep_alive = keep_alive;
	file_data->path = path_ptr;

	if (r_fd)
		*r_fd = fd;

	return rc;
out:
	free(path_ptr);
	close(fd);
	return rc;
}

void file_exit(struct blocklevel_device *bl)
{
	struct file_data *file_data;
	if (bl) {
		free(bl->ecc_prot.prot);
		file_data = container_of(bl, struct file_data, bl);
		free(file_data->name);
		free(file_data->path);
		free(file_data);
	}
}

void file_exit_close(struct blocklevel_device *bl)
{
	struct file_data *file_data;
	if (bl) {
		file_data = container_of(bl, struct file_data, bl);
		close(file_data->fd);
		file_exit(bl);
	}
}