[PATCH 08/44 take 2] [UBI] misc unit implementation

Artem Bityutskiy
2007-02-17T17:02:20Z

diff -auNrp tmp-from/drivers/mtd/ubi/misc.c tmp-to/drivers/mtd/ubi/misc.c
--- tmp-from/drivers/mtd/ubi/misc.c 1970-01-01 02:00:00.000000000 +0200
+++ tmp-to/drivers/mtd/ubi/misc.c 2007-02-17 18:07:26.000000000 +0200
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) International Business Machines Corp., 2006
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Artem B. Bityutskiy
+ */
+
+#include <linux/string.h>
+#include <linux/sched.h>
+#include "ubi.h"
+#include "misc.h"
+#include "vtbl.h"
+#include "debug.h"
+#include "eba.h"
+#include "alloc.h"
+#include "io.h"
+#include "account.h"
+#include "background.h"
+
+int ubi_buf_all_ff(const void *buf, int size)
+{
+ int i;
+
+ for (i = 0; i < size / sizeof(unsigned int); i++)
+ if (((const unsigned int *)buf)[i] != ~0)
+ return 0;
+
+ for (i = i; i < size; i++)
+ if (((const uint8_t *)buf)[i] != 0xFF)
+ return 0;
+
+ return 1;
+}
+
+int ubi_buf_all_zeroes(const void *buf, int size)
+{
+ int i;
+
+ for (i = 0; i < size / sizeof(unsigned int); i++)
+ if (((const unsigned int *)buf)[i] != 0)
+ return 0;
+
+ for (i = i; i < size; i++)
+ if (((const uint8_t *)buf)[i] != 0)
+ return 0;
+
+ return 1;
+}
+
+int ubi_check_pattern(const void *buf, uint8_t patt, int size)
+{
+ int i;
+
+ for (i = 0; i < size; i++)
+ if (((const uint8_t *)buf)[i] != patt)
+ return 0;
+ return 1;
+}
+
+char *strdup_len(const char *str, int len)
+{
+ char *dup;
+
+ ubi_assert(strnlen(str, len + 1) == len);
+
+ dup = ubi_kmalloc(len + 1);
+ if (!dup)
+ return NULL;
+
+ memcpy(dup, str, len);
+ dup[len] = '\0';
+
+ return dup;
+}
+
+int ubi_calc_data_len(const struct ubi_info *ubi, const void *buf,
+ int length)
+{
+ int i;
+
+ ubi_assert(length % ubi->io->min_io_size == 0);
+
+ for (i = length - 1; i >= 0; i--)
+ if (((const uint8_t *)buf)[i] != 0xFF)
+ break;
+
+ /* The resulting length must be aligned to the minimum flash I/O size */
+ length = align_up(i + 1, ubi->io->min_io_size);
+ return length;
+}
+
+int ubi_check_volume(const struct ubi_info *ubi, int vol_id)
+{
+ void *buf;
+ int err = 0, i;
+ const struct ubi_vtbl_vtr *vtr;
+
+ vtr = ubi_vtbl_get_vtr(ubi, vol_id);
+
+ if (vtr->vol_type != UBI_STATIC_VOLUME)
+ return 0;
+
+ buf = ubi_kmalloc(vtr->usable_leb_size);
+ if (!buf)
+ return -ENOMEM;
+
+ for (i = 0; i < vtr->used_ebs; i++) {
+ int size;
+
+ if (i == vtr->used_ebs - 1)
+ size = vtr->last_eb_bytes;
+ else
+ size = vtr->usable_leb_size;
+
+ err = ubi_eba_read_leb(ubi, vol_id, i, buf, 0, size, 1);
+ if (err) {
+ if (err == -EBADMSG)
+ err = 1;
+ break;
+ }
+ }
+
+ ubi_kfree(buf);
+ return err;
+}