blob: 28a362aa14a9a85164b002186c3864f8cf95e239 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kyungmin Park7f88f002008-11-19 16:28:06 +01002/*
3 * Copyright (c) International Business Machines Corp., 2006
4 * Copyright (c) Nokia Corporation, 2006
5 *
Kyungmin Park7f88f002008-11-19 16:28:06 +01006 * 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
Heiko Schocherf5895d12014-06-24 10:10:04 +020028#ifndef __UBOOT__
Simon Glass0f2af882020-05-10 11:40:05 -060029#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070030#include <malloc.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020031#include <linux/uaccess.h>
32#else
33#include <div64.h>
34#include <ubi_uboot.h>
Kyungmin Park7f88f002008-11-19 16:28:06 +010035#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +020036#include <linux/err.h>
37#include <linux/math64.h>
Kyungmin Park7f88f002008-11-19 16:28:06 +010038
Kyungmin Park7f88f002008-11-19 16:28:06 +010039#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 */
49static int set_update_marker(struct ubi_device *ubi, struct ubi_volume *vol)
50{
51 int err;
52 struct ubi_vtbl_record vtbl_rec;
53
Heiko Schocherf5895d12014-06-24 10:10:04 +020054 dbg_gen("set update marker for volume %d", vol->vol_id);
Kyungmin Park7f88f002008-11-19 16:28:06 +010055
56 if (vol->upd_marker) {
57 ubi_assert(ubi->vtbl[vol->vol_id].upd_marker);
Heiko Schocherf5895d12014-06-24 10:10:04 +020058 dbg_gen("already set");
Kyungmin Park7f88f002008-11-19 16:28:06 +010059 return 0;
60 }
61
Heiko Schocherf5895d12014-06-24 10:10:04 +020062 vtbl_rec = ubi->vtbl[vol->vol_id];
Kyungmin Park7f88f002008-11-19 16:28:06 +010063 vtbl_rec.upd_marker = 1;
64
Heiko Schocherf5895d12014-06-24 10:10:04 +020065 mutex_lock(&ubi->device_mutex);
Kyungmin Park7f88f002008-11-19 16:28:06 +010066 err = ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
Kyungmin Park7f88f002008-11-19 16:28:06 +010067 vol->upd_marker = 1;
Heiko Schocherf5895d12014-06-24 10:10:04 +020068 mutex_unlock(&ubi->device_mutex);
Kyungmin Park7f88f002008-11-19 16:28:06 +010069 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 */
82static int clear_update_marker(struct ubi_device *ubi, struct ubi_volume *vol,
83 long long bytes)
84{
85 int err;
Kyungmin Park7f88f002008-11-19 16:28:06 +010086 struct ubi_vtbl_record vtbl_rec;
87
Heiko Schocherf5895d12014-06-24 10:10:04 +020088 dbg_gen("clear update marker for volume %d", vol->vol_id);
Kyungmin Park7f88f002008-11-19 16:28:06 +010089
Heiko Schocherf5895d12014-06-24 10:10:04 +020090 vtbl_rec = ubi->vtbl[vol->vol_id];
Kyungmin Park7f88f002008-11-19 16:28:06 +010091 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;
Heiko Schocherf5895d12014-06-24 10:10:04 +020096 vol->used_bytes = bytes;
97 vol->used_ebs = div_u64_rem(bytes, vol->usable_leb_size,
98 &vol->last_eb_bytes);
Kyungmin Park7f88f002008-11-19 16:28:06 +010099 if (vol->last_eb_bytes)
100 vol->used_ebs += 1;
101 else
102 vol->last_eb_bytes = vol->usable_leb_size;
103 }
104
Heiko Schocherf5895d12014-06-24 10:10:04 +0200105 mutex_lock(&ubi->device_mutex);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100106 err = ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100107 vol->upd_marker = 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200108 mutex_unlock(&ubi->device_mutex);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100109 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 */
122int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
123 long long bytes)
124{
125 int i, err;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100126
Heiko Schocherf5895d12014-06-24 10:10:04 +0200127 dbg_gen("start update of volume %d, %llu bytes", vol->vol_id, bytes);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100128 ubi_assert(!vol->updating && !vol->changing_leb);
129 vol->updating = 1;
130
Heiko Schocher94b66de2015-10-22 06:19:21 +0200131 vol->upd_buf = vmalloc(ubi->leb_size);
132 if (!vol->upd_buf)
133 return -ENOMEM;
134
Kyungmin Park7f88f002008-11-19 16:28:06 +0100135 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) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200147 err = ubi_wl_flush(ubi, UBI_ALL, UBI_ALL);
148 if (err)
149 return err;
150
Kyungmin Park7f88f002008-11-19 16:28:06 +0100151 err = clear_update_marker(ubi, vol, 0);
152 if (err)
153 return err;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200154
155 vfree(vol->upd_buf);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200156 vol->updating = 0;
157 return 0;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100158 }
159
Heiko Schocherf5895d12014-06-24 10:10:04 +0200160 vol->upd_ebs = div_u64(bytes + vol->usable_leb_size - 1,
161 vol->usable_leb_size);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100162 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 */
176int 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
Heiko Schocherf5895d12014-06-24 10:10:04 +0200181 dbg_gen("start changing LEB %d:%d, %u bytes",
Kyungmin Park7f88f002008-11-19 16:28:06 +0100182 vol->vol_id, req->lnum, req->bytes);
183 if (req->bytes == 0)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200184 return ubi_eba_atomic_leb_change(ubi, vol, req->lnum, NULL, 0);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100185
186 vol->upd_bytes = req->bytes;
187 vol->upd_received = 0;
188 vol->changing_leb = 1;
189 vol->ch_lnum = req->lnum;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100190
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 */
227static 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) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200238 dbg_gen("all %d bytes contain 0xFF - skip", len);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100239 return 0;
240 }
241
Heiko Schocherf5895d12014-06-24 10:10:04 +0200242 err = ubi_eba_write_leb(ubi, vol, lnum, buf, 0, len);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100243 } 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);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200254 err = ubi_eba_write_leb_st(ubi, vol, lnum, buf, len, used_ebs);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100255 }
256
257 return err;
258}
259
260/**
261 * ubi_more_update_data - write more update data.
Heiko Schocherf5895d12014-06-24 10:10:04 +0200262 * @ubi: UBI device description object
Kyungmin Park7f88f002008-11-19 16:28:06 +0100263 * @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 */
273int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
274 const void __user *buf, int count)
275{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200276#ifndef __UBOOT__
Kyungmin Park7f88f002008-11-19 16:28:06 +0100277 int lnum, offs, err = 0, len, to_write = count;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200278#else
279 int lnum, err = 0, len, to_write = count;
280 u32 offs;
281#endif
Kyungmin Park7f88f002008-11-19 16:28:06 +0100282
Heiko Schocherf5895d12014-06-24 10:10:04 +0200283 dbg_gen("write %d of %lld bytes, %lld already passed",
Kyungmin Park7f88f002008-11-19 16:28:06 +0100284 count, vol->upd_bytes, vol->upd_received);
285
286 if (ubi->ro_mode)
287 return -EROFS;
288
Heiko Schocherf5895d12014-06-24 10:10:04 +0200289 lnum = div_u64_rem(vol->upd_received, vol->usable_leb_size, &offs);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100290 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) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200364 err = ubi_wl_flush(ubi, UBI_ALL, UBI_ALL);
365 if (err)
366 return err;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100367 /* The update is finished, clear the update marker */
368 err = clear_update_marker(ubi, vol, vol->upd_bytes);
369 if (err)
370 return err;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200371 vol->updating = 0;
372 err = to_write;
373 vfree(vol->upd_buf);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100374 }
375
376 return err;
377}
378
379/**
380 * ubi_more_leb_change_data - accept more data for atomic LEB change.
Heiko Schocherf5895d12014-06-24 10:10:04 +0200381 * @ubi: UBI device description object
Kyungmin Park7f88f002008-11-19 16:28:06 +0100382 * @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 */
393int 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
Heiko Schocherf5895d12014-06-24 10:10:04 +0200398 dbg_gen("write %d of %lld bytes, %lld already passed",
Kyungmin Park7f88f002008-11-19 16:28:06 +0100399 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
Heiko Schocherf5895d12014-06-24 10:10:04 +0200416 memset(vol->upd_buf + vol->upd_bytes, 0xFF,
417 len - vol->upd_bytes);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100418 len = ubi_calc_data_len(ubi, vol->upd_buf, len);
419 err = ubi_eba_atomic_leb_change(ubi, vol, vol->ch_lnum,
Heiko Schocherf5895d12014-06-24 10:10:04 +0200420 vol->upd_buf, len);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100421 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}