1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) International Business Machines Corp., 2006
4  * Copyright (c) Nokia Corporation, 2006
5  *
6  * Author: Artem Bityutskiy (Битюцкий Артём)
7  *
8  * Jan 2007: Alexander Schmidt, hacked per-volume update.
9  */
10 
11 /*
12  * This file contains implementation of the volume update and atomic LEB change
13  * functionality.
14  *
15  * The update operation is based on the per-volume update marker which is
16  * stored in the volume table. The update marker is set before the update
17  * starts, and removed after the update has been finished. So if the update was
18  * interrupted by an unclean re-boot or due to some other reasons, the update
19  * marker stays on the flash media and UBI finds it when it attaches the MTD
20  * device next time. If the update marker is set for a volume, the volume is
21  * treated as damaged and most I/O operations are prohibited. Only a new update
22  * operation is allowed.
23  *
24  * Note, in general it is possible to implement the update operation as a
25  * transaction with a roll-back capability.
26  */
27 
28 #ifndef __UBOOT__
29 #include <log.h>
30 #include <malloc.h>
31 #include <linux/uaccess.h>
32 #else
33 #include <div64.h>
34 #include <ubi_uboot.h>
35 #endif
36 #include <linux/err.h>
37 #include <linux/math64.h>
38 
39 #include "ubi.h"
40 
41 /**
42  * set_update_marker - set update marker.
43  * @ubi: UBI device description object
44  * @vol: volume description object
45  *
46  * This function sets the update marker flag for volume @vol. Returns zero
47  * in case of success and a negative error code in case of failure.
48  */
set_update_marker(struct ubi_device * ubi,struct ubi_volume * vol)49 static int set_update_marker(struct ubi_device *ubi, struct ubi_volume *vol)
50 {
51 	int err;
52 	struct ubi_vtbl_record vtbl_rec;
53 
54 	dbg_gen("set update marker for volume %d", vol->vol_id);
55 
56 	if (vol->upd_marker) {
57 		ubi_assert(ubi->vtbl[vol->vol_id].upd_marker);
58 		dbg_gen("already set");
59 		return 0;
60 	}
61 
62 	vtbl_rec = ubi->vtbl[vol->vol_id];
63 	vtbl_rec.upd_marker = 1;
64 
65 	mutex_lock(&ubi->device_mutex);
66 	err = ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
67 	vol->upd_marker = 1;
68 	mutex_unlock(&ubi->device_mutex);
69 	return err;
70 }
71 
72 /**
73  * clear_update_marker - clear update marker.
74  * @ubi: UBI device description object
75  * @vol: volume description object
76  * @bytes: new data size in bytes
77  *
78  * This function clears the update marker for volume @vol, sets new volume
79  * data size and clears the "corrupted" flag (static volumes only). Returns
80  * zero in case of success and a negative error code in case of failure.
81  */
clear_update_marker(struct ubi_device * ubi,struct ubi_volume * vol,long long bytes)82 static int clear_update_marker(struct ubi_device *ubi, struct ubi_volume *vol,
83 			       long long bytes)
84 {
85 	int err;
86 	struct ubi_vtbl_record vtbl_rec;
87 
88 	dbg_gen("clear update marker for volume %d", vol->vol_id);
89 
90 	vtbl_rec = ubi->vtbl[vol->vol_id];
91 	ubi_assert(vol->upd_marker && vtbl_rec.upd_marker);
92 	vtbl_rec.upd_marker = 0;
93 
94 	if (vol->vol_type == UBI_STATIC_VOLUME) {
95 		vol->corrupted = 0;
96 		vol->used_bytes = bytes;
97 		vol->used_ebs = div_u64_rem(bytes, vol->usable_leb_size,
98 					    &vol->last_eb_bytes);
99 		if (vol->last_eb_bytes)
100 			vol->used_ebs += 1;
101 		else
102 			vol->last_eb_bytes = vol->usable_leb_size;
103 	}
104 
105 	mutex_lock(&ubi->device_mutex);
106 	err = ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
107 	vol->upd_marker = 0;
108 	mutex_unlock(&ubi->device_mutex);
109 	return err;
110 }
111 
112 /**
113  * ubi_start_update - start volume update.
114  * @ubi: UBI device description object
115  * @vol: volume description object
116  * @bytes: update bytes
117  *
118  * This function starts volume update operation. If @bytes is zero, the volume
119  * is just wiped out. Returns zero in case of success and a negative error code
120  * in case of failure.
121  */
ubi_start_update(struct ubi_device * ubi,struct ubi_volume * vol,long long bytes)122 int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
123 		     long long bytes)
124 {
125 	int i, err;
126 
127 	dbg_gen("start update of volume %d, %llu bytes", vol->vol_id, bytes);
128 	ubi_assert(!vol->updating && !vol->changing_leb);
129 	vol->updating = 1;
130 
131 	vol->upd_buf = vmalloc(ubi->leb_size);
132 	if (!vol->upd_buf)
133 		return -ENOMEM;
134 
135 	err = set_update_marker(ubi, vol);
136 	if (err)
137 		return err;
138 
139 	/* Before updating - wipe out the volume */
140 	for (i = 0; i < vol->reserved_pebs; i++) {
141 		err = ubi_eba_unmap_leb(ubi, vol, i);
142 		if (err)
143 			return err;
144 	}
145 
146 	if (bytes == 0) {
147 		err = ubi_wl_flush(ubi, UBI_ALL, UBI_ALL);
148 		if (err)
149 			return err;
150 
151 		err = clear_update_marker(ubi, vol, 0);
152 		if (err)
153 			return err;
154 
155 		vfree(vol->upd_buf);
156 		vol->updating = 0;
157 		return 0;
158 	}
159 
160 	vol->upd_ebs = div_u64(bytes + vol->usable_leb_size - 1,
161 			       vol->usable_leb_size);
162 	vol->upd_bytes = bytes;
163 	vol->upd_received = 0;
164 	return 0;
165 }
166 
167 /**
168  * ubi_start_leb_change - start atomic LEB change.
169  * @ubi: UBI device description object
170  * @vol: volume description object
171  * @req: operation request
172  *
173  * This function starts atomic LEB change operation. Returns zero in case of
174  * success and a negative error code in case of failure.
175  */
ubi_start_leb_change(struct ubi_device * ubi,struct ubi_volume * vol,const struct ubi_leb_change_req * req)176 int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
177 			 const struct ubi_leb_change_req *req)
178 {
179 	ubi_assert(!vol->updating && !vol->changing_leb);
180 
181 	dbg_gen("start changing LEB %d:%d, %u bytes",
182 		vol->vol_id, req->lnum, req->bytes);
183 	if (req->bytes == 0)
184 		return ubi_eba_atomic_leb_change(ubi, vol, req->lnum, NULL, 0);
185 
186 	vol->upd_bytes = req->bytes;
187 	vol->upd_received = 0;
188 	vol->changing_leb = 1;
189 	vol->ch_lnum = req->lnum;
190 
191 	vol->upd_buf = vmalloc(req->bytes);
192 	if (!vol->upd_buf)
193 		return -ENOMEM;
194 
195 	return 0;
196 }
197 
198 /**
199  * write_leb - write update data.
200  * @ubi: UBI device description object
201  * @vol: volume description object
202  * @lnum: logical eraseblock number
203  * @buf: data to write
204  * @len: data size
205  * @used_ebs: how many logical eraseblocks will this volume contain (static
206  * volumes only)
207  *
208  * This function writes update data to corresponding logical eraseblock. In
209  * case of dynamic volume, this function checks if the data contains 0xFF bytes
210  * at the end. If yes, the 0xFF bytes are cut and not written. So if the whole
211  * buffer contains only 0xFF bytes, the LEB is left unmapped.
212  *
213  * The reason why we skip the trailing 0xFF bytes in case of dynamic volume is
214  * that we want to make sure that more data may be appended to the logical
215  * eraseblock in future. Indeed, writing 0xFF bytes may have side effects and
216  * this PEB won't be writable anymore. So if one writes the file-system image
217  * to the UBI volume where 0xFFs mean free space - UBI makes sure this free
218  * space is writable after the update.
219  *
220  * We do not do this for static volumes because they are read-only. But this
221  * also cannot be done because we have to store per-LEB CRC and the correct
222  * data length.
223  *
224  * This function returns zero in case of success and a negative error code in
225  * case of failure.
226  */
write_leb(struct ubi_device * ubi,struct ubi_volume * vol,int lnum,void * buf,int len,int used_ebs)227 static int write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
228 		     void *buf, int len, int used_ebs)
229 {
230 	int err;
231 
232 	if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
233 		int l = ALIGN(len, ubi->min_io_size);
234 
235 		memset(buf + len, 0xFF, l - len);
236 		len = ubi_calc_data_len(ubi, buf, l);
237 		if (len == 0) {
238 			dbg_gen("all %d bytes contain 0xFF - skip", len);
239 			return 0;
240 		}
241 
242 		err = ubi_eba_write_leb(ubi, vol, lnum, buf, 0, len);
243 	} else {
244 		/*
245 		 * When writing static volume, and this is the last logical
246 		 * eraseblock, the length (@len) does not have to be aligned to
247 		 * the minimal flash I/O unit. The 'ubi_eba_write_leb_st()'
248 		 * function accepts exact (unaligned) length and stores it in
249 		 * the VID header. And it takes care of proper alignment by
250 		 * padding the buffer. Here we just make sure the padding will
251 		 * contain zeros, not random trash.
252 		 */
253 		memset(buf + len, 0, vol->usable_leb_size - len);
254 		err = ubi_eba_write_leb_st(ubi, vol, lnum, buf, len, used_ebs);
255 	}
256 
257 	return err;
258 }
259 
260 /**
261  * ubi_more_update_data - write more update data.
262  * @ubi: UBI device description object
263  * @vol: volume description object
264  * @buf: write data (user-space memory buffer)
265  * @count: how much bytes to write
266  *
267  * This function writes more data to the volume which is being updated. It may
268  * be called arbitrary number of times until all the update data arriveis. This
269  * function returns %0 in case of success, number of bytes written during the
270  * last call if the whole volume update has been successfully finished, and a
271  * negative error code in case of failure.
272  */
ubi_more_update_data(struct ubi_device * ubi,struct ubi_volume * vol,const void __user * buf,int count)273 int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
274 			 const void __user *buf, int count)
275 {
276 #ifndef __UBOOT__
277 	int lnum, offs, err = 0, len, to_write = count;
278 #else
279 	int lnum, err = 0, len, to_write = count;
280 	u32 offs;
281 #endif
282 
283 	dbg_gen("write %d of %lld bytes, %lld already passed",
284 		count, vol->upd_bytes, vol->upd_received);
285 
286 	if (ubi->ro_mode)
287 		return -EROFS;
288 
289 	lnum = div_u64_rem(vol->upd_received,  vol->usable_leb_size, &offs);
290 	if (vol->upd_received + count > vol->upd_bytes)
291 		to_write = count = vol->upd_bytes - vol->upd_received;
292 
293 	/*
294 	 * When updating volumes, we accumulate whole logical eraseblock of
295 	 * data and write it at once.
296 	 */
297 	if (offs != 0) {
298 		/*
299 		 * This is a write to the middle of the logical eraseblock. We
300 		 * copy the data to our update buffer and wait for more data or
301 		 * flush it if the whole eraseblock is written or the update
302 		 * is finished.
303 		 */
304 
305 		len = vol->usable_leb_size - offs;
306 		if (len > count)
307 			len = count;
308 
309 		err = copy_from_user(vol->upd_buf + offs, buf, len);
310 		if (err)
311 			return -EFAULT;
312 
313 		if (offs + len == vol->usable_leb_size ||
314 		    vol->upd_received + len == vol->upd_bytes) {
315 			int flush_len = offs + len;
316 
317 			/*
318 			 * OK, we gathered either the whole eraseblock or this
319 			 * is the last chunk, it's time to flush the buffer.
320 			 */
321 			ubi_assert(flush_len <= vol->usable_leb_size);
322 			err = write_leb(ubi, vol, lnum, vol->upd_buf, flush_len,
323 					vol->upd_ebs);
324 			if (err)
325 				return err;
326 		}
327 
328 		vol->upd_received += len;
329 		count -= len;
330 		buf += len;
331 		lnum += 1;
332 	}
333 
334 	/*
335 	 * If we've got more to write, let's continue. At this point we know we
336 	 * are starting from the beginning of an eraseblock.
337 	 */
338 	while (count) {
339 		if (count > vol->usable_leb_size)
340 			len = vol->usable_leb_size;
341 		else
342 			len = count;
343 
344 		err = copy_from_user(vol->upd_buf, buf, len);
345 		if (err)
346 			return -EFAULT;
347 
348 		if (len == vol->usable_leb_size ||
349 		    vol->upd_received + len == vol->upd_bytes) {
350 			err = write_leb(ubi, vol, lnum, vol->upd_buf,
351 					len, vol->upd_ebs);
352 			if (err)
353 				break;
354 		}
355 
356 		vol->upd_received += len;
357 		count -= len;
358 		lnum += 1;
359 		buf += len;
360 	}
361 
362 	ubi_assert(vol->upd_received <= vol->upd_bytes);
363 	if (vol->upd_received == vol->upd_bytes) {
364 		err = ubi_wl_flush(ubi, UBI_ALL, UBI_ALL);
365 		if (err)
366 			return err;
367 		/* The update is finished, clear the update marker */
368 		err = clear_update_marker(ubi, vol, vol->upd_bytes);
369 		if (err)
370 			return err;
371 		vol->updating = 0;
372 		err = to_write;
373 		vfree(vol->upd_buf);
374 	}
375 
376 	return err;
377 }
378 
379 /**
380  * ubi_more_leb_change_data - accept more data for atomic LEB change.
381  * @ubi: UBI device description object
382  * @vol: volume description object
383  * @buf: write data (user-space memory buffer)
384  * @count: how much bytes to write
385  *
386  * This function accepts more data to the volume which is being under the
387  * "atomic LEB change" operation. It may be called arbitrary number of times
388  * until all data arrives. This function returns %0 in case of success, number
389  * of bytes written during the last call if the whole "atomic LEB change"
390  * operation has been successfully finished, and a negative error code in case
391  * of failure.
392  */
ubi_more_leb_change_data(struct ubi_device * ubi,struct ubi_volume * vol,const void __user * buf,int count)393 int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,
394 			     const void __user *buf, int count)
395 {
396 	int err;
397 
398 	dbg_gen("write %d of %lld bytes, %lld already passed",
399 		count, vol->upd_bytes, vol->upd_received);
400 
401 	if (ubi->ro_mode)
402 		return -EROFS;
403 
404 	if (vol->upd_received + count > vol->upd_bytes)
405 		count = vol->upd_bytes - vol->upd_received;
406 
407 	err = copy_from_user(vol->upd_buf + vol->upd_received, buf, count);
408 	if (err)
409 		return -EFAULT;
410 
411 	vol->upd_received += count;
412 
413 	if (vol->upd_received == vol->upd_bytes) {
414 		int len = ALIGN((int)vol->upd_bytes, ubi->min_io_size);
415 
416 		memset(vol->upd_buf + vol->upd_bytes, 0xFF,
417 		       len - vol->upd_bytes);
418 		len = ubi_calc_data_len(ubi, vol->upd_buf, len);
419 		err = ubi_eba_atomic_leb_change(ubi, vol, vol->ch_lnum,
420 						vol->upd_buf, len);
421 		if (err)
422 			return err;
423 	}
424 
425 	ubi_assert(vol->upd_received <= vol->upd_bytes);
426 	if (vol->upd_received == vol->upd_bytes) {
427 		vol->changing_leb = 0;
428 		err = count;
429 		vfree(vol->upd_buf);
430 	}
431 
432 	return err;
433 }
434