embox

Форк
0
/
flashio.c 
166 строк · 4.2 Кб
1
/*
2
 * JFFS2 -- Journalling Flash File System, Version 2.
3
 *
4
 * Copyright (C) 2001-2003 Red Hat, Inc.
5
 *
6
 * Created by Dominic Ostrowski <dominic.ostrowski@3glab.com>
7
 * Contributors: David Woodhouse, Nick Garnett, Richard Panton.
8
 *
9
 * For licensing information, see the file 'LICENCE' in this directory.
10
 *
11
 * $Id: flashio.c,v 1.1 2003/11/26 14:09:29 dwmw2 Exp $
12
 *
13
 */
14

15
#include <linux/kernel.h>
16
#include "nodelist.h"
17

18
#include <drivers/block_dev.h>
19
#include <drivers/flash/flash.h>
20
#include <drivers/block_dev.h>
21
#include <fs/super_block.h>
22
#include <mem/sysmalloc.h>
23

24
bool jffs2_flash_read(struct jffs2_sb_info * c,
25
		uint32_t read_buffer_offset, const size_t size,
26
			  size_t * return_size, unsigned char *write_buffer) {
27
	int err;
28
	struct jffs2_super_block *sb;
29
	sb = member_cast_out(c, struct jffs2_super_block, jffs2_sb);
30

31
	err = block_dev_read_buffered(sb->bdev,
32
			(char *) write_buffer, size, read_buffer_offset);
33

34
	if(err >= 0) {
35
		*return_size = (size_t) err;
36
		err = ENOERR;
37
	}
38

39
	return err;
40
}
41

42
bool jffs2_flash_write(struct jffs2_sb_info * c,
43
		uint32_t write_buffer_offset, const size_t size,
44
		size_t * return_size, unsigned char *read_buffer) {
45
	int err;
46
	struct jffs2_super_block *sb;
47

48
	sb = member_cast_out(c, struct jffs2_super_block, jffs2_sb);
49
	err = block_dev_write_buffered(sb->bdev,
50
	(char *) read_buffer, size, write_buffer_offset);
51

52
	if(err >= 0) {
53
		*return_size = (size_t) err;
54
		err = ENOERR;
55
	}
56

57
	return err;
58
}
59

60
int jffs2_flash_direct_writev(struct jffs2_sb_info *c, const struct iovec *vecs,
61
		   unsigned long count, loff_t to, size_t * retlen) {
62
	unsigned long i;
63
	size_t totlen = 0, thislen;
64
	int ret = 0;
65

66
	for (i = 0; i < count; i++) {
67
		/*
68
		 * writes need to be aligned but the data we're passed may not be
69
		 * Observation suggests most unaligned writes are small, so we
70
		 *  optimize for that case.
71
		 */
72

73
		if (((vecs[i].iov_len & (sizeof (int) - 1))) ||
74
		    (((unsigned long) vecs[i].
75
		      iov_base & (sizeof (unsigned long) - 1)))) {
76
			/* are there iov's after this one? Or is it so much we'd need
77
			 * to do multiple writes anyway?
78
			 */
79
			if ((i + 1) < count || vecs[i].iov_len > 256) {
80
				/* cop out and kmalloc */
81
				unsigned long j;
82
				ssize_t sizetomalloc = 0, totvecsize = 0;
83
				char *cbuf, *cbufptr;
84

85
				for (j = i; j < count; j++) {
86
					totvecsize += vecs[j].iov_len;
87
				}
88

89
				/* pad up in case unaligned */
90
				sizetomalloc = totvecsize + sizeof (int) - 1;
91
				sizetomalloc &= ~(sizeof (int) - 1);
92
				cbuf = (char *) sysmalloc(sizetomalloc);
93
				/* kmalloc returns aligned memory */
94
				if (!cbuf) {
95
					ret = -ENOMEM;
96
					goto writev_out;
97
				}
98
				cbufptr = cbuf;
99
				for (j = i; j < count; j++) {
100
					memcpy(cbufptr, vecs[j].iov_base,
101
					       vecs[j].iov_len);
102
					cbufptr += vecs[j].iov_len;
103
				}
104
				ret =
105
				    jffs2_flash_write(c, to, sizetomalloc,
106
						      &thislen, (unsigned char *) cbuf);
107
				if (thislen > totvecsize) {	/* in case it was aligned up */
108
					thislen = totvecsize;
109
				}
110
				totlen += thislen;
111
				sysfree(cbuf);
112
				goto writev_out;
113
			} else {
114
				/* otherwise optimize for the common case */
115
				int buf[256 / sizeof (int)];	/* int, so int aligned */
116
				size_t lentowrite;
117

118
				lentowrite = vecs[i].iov_len;
119
				/* pad up in case its unaligned */
120
				lentowrite += sizeof (int) - 1;
121
				lentowrite &= ~(sizeof (int) - 1);
122
				memcpy(buf, vecs[i].iov_base, lentowrite);
123

124
				ret =
125
				    jffs2_flash_write(c, to, lentowrite,
126
						      &thislen, (unsigned char *) &buf);
127
				if (thislen > vecs[i].iov_len) {
128
					thislen = vecs[i].iov_len;
129
				}
130
			}
131
		} else {
132
			ret =
133
			    jffs2_flash_write(c, to, vecs[i].iov_len, &thislen,
134
					      vecs[i].iov_base);
135
		}
136
		totlen += thislen;
137
		if (ret || thislen != vecs[i].iov_len) {
138
			break;
139
		}
140
		to += vecs[i].iov_len;
141
	}
142
      writev_out:
143
	if (retlen) {
144
		*retlen = totlen;
145
	}
146

147
	return ret;
148
}
149

150
bool jffs2_flash_erase(struct jffs2_sb_info * c,
151
			   struct jffs2_eraseblock * jeb) {
152
	struct flash_ioctl_erase e;
153
	uint32_t err_addr;
154
	int err;
155
	uint32_t len = sizeof (e);
156
	struct jffs2_super_block *sb;
157
	sb = member_cast_out(c, struct jffs2_super_block, jffs2_sb);
158

159
	e.offset = jeb->offset;
160
	e.len = c->sector_size;
161
	e.err_address = (uint32_t) &err_addr;
162

163
	err = block_dev_ioctl(sb->bdev, FLASH_IOCTL_ERASE, &e, len);
164

165
	return (err != ENOERR || e.flasherr != 0);
166
}
167

168

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.