diff options
Diffstat (limited to 'ubifs-utils')
-rw-r--r-- | ubifs-utils/Makemodule.am | 26 | ||||
-rw-r--r-- | ubifs-utils/mkfs.ubifs/compr.c | 39 | ||||
-rw-r--r-- | ubifs-utils/mkfs.ubifs/fscrypt.h | 4 | ||||
-rw-r--r-- | ubifs-utils/mkfs.ubifs/mkfs.ubifs.c | 57 | ||||
-rw-r--r-- | ubifs-utils/mkfs.ubifs/sign.c | 3 | ||||
-rwxr-xr-x | ubifs-utils/mount.ubifs | 101 |
6 files changed, 173 insertions, 57 deletions
diff --git a/ubifs-utils/Makemodule.am b/ubifs-utils/Makemodule.am index 59109cc..6814d47 100644 --- a/ubifs-utils/Makemodule.am +++ b/ubifs-utils/Makemodule.am @@ -16,11 +16,14 @@ mkfs_ubifs_SOURCES = \ ubifs-utils/mkfs.ubifs/hashtable/hashtable_private.h \ ubifs-utils/mkfs.ubifs/hashtable/hashtable.c \ ubifs-utils/mkfs.ubifs/hashtable/hashtable_itr.c \ - ubifs-utils/mkfs.ubifs/devtable.c + ubifs-utils/mkfs.ubifs/devtable.c \ + include/mtd/ubifs-media.h if WITH_CRYPTO mkfs_ubifs_SOURCES += ubifs-utils/mkfs.ubifs/crypto.c \ + ubifs-utils/mkfs.ubifs/crypto.h \ ubifs-utils/mkfs.ubifs/fscrypt.c \ + ubifs-utils/mkfs.ubifs/fscrypt.h \ ubifs-utils/mkfs.ubifs/sign.c endif @@ -28,23 +31,8 @@ mkfs_ubifs_LDADD = libmtd.a libubi.a $(ZLIB_LIBS) $(LZO_LIBS) $(ZSTD_LIBS) $(UUI mkfs_ubifs_CPPFLAGS = $(AM_CPPFLAGS) $(ZLIB_CFLAGS) $(LZO_CFLAGS) $(ZSTD_CFLAGS) $(UUID_CFLAGS) $(LIBSELINUX_CFLAGS)\ -I$(top_srcdir)/ubi-utils/include -I$(top_srcdir)/ubifs-utils/mkfs.ubifs/ -UBIFS_BINS = \ - mkfs.ubifs +EXTRA_DIST += ubifs-utils/mkfs.ubifs/README -UBIFS_HEADER = \ - ubifs-utils/mkfs.ubifs/compr.h ubifs-utils/mkfs.ubifs/crc16.h \ - ubifs-utils/mkfs.ubifs/defs.h ubifs-utils/mkfs.ubifs/key.h \ - ubifs-utils/mkfs.ubifs/lpt.h ubifs-utils/mkfs.ubifs/mkfs.ubifs.h \ - ubifs-utils/mkfs.ubifs/ubifs.h \ - ubifs-utils/mkfs.ubifs/crypto.h \ - ubifs-utils/mkfs.ubifs/fscrypt.h \ - ubifs-utils/mkfs.ubifs/hashtable/hashtable.h \ - ubifs-utils/mkfs.ubifs/hashtable/hashtable_itr.h \ - ubifs-utils/mkfs.ubifs/hashtable/hashtable_private.h - -UBIFS_EXTRA = \ - ubifs-utils/mkfs.ubifs/README - -EXTRA_DIST += $(UBIFS_HEADER) $(UBIFS_EXTRA) +dist_sbin_SCRIPTS = ubifs-utils/mount.ubifs -sbin_PROGRAMS += $(UBIFS_BINS) +sbin_PROGRAMS += mkfs.ubifs diff --git a/ubifs-utils/mkfs.ubifs/compr.c b/ubifs-utils/mkfs.ubifs/compr.c index 06c35ca..e4324f3 100644 --- a/ubifs-utils/mkfs.ubifs/compr.c +++ b/ubifs-utils/mkfs.ubifs/compr.c @@ -24,27 +24,30 @@ #include <stdio.h> #include <stdint.h> #include <string.h> -#ifndef WITHOUT_LZO +#ifdef WITH_LZO #include <lzo/lzo1x.h> #endif #include <linux/types.h> -#ifndef WITHOUT_ZSTD +#ifdef WITH_ZSTD #include <zstd.h> #endif +#ifdef WITH_ZLIB #define crc32 __zlib_crc32 #include <zlib.h> #undef crc32 +#endif #include "compr.h" #include "mkfs.ubifs.h" static void *lzo_mem; static unsigned long long errcnt = 0; -#ifndef WITHOUT_LZO +#ifdef WITH_LZO static struct ubifs_info *c = &info_; #endif +#ifdef WITH_ZLIB #define DEFLATE_DEF_LEVEL Z_DEFAULT_COMPRESSION #define DEFLATE_DEF_WINBITS 11 #define DEFLATE_DEF_MEMLEVEL 8 @@ -91,8 +94,9 @@ static int zlib_deflate(void *in_buf, size_t in_len, void *out_buf, return 0; } +#endif -#ifndef WITHOUT_LZO +#ifdef WITH_LZO static int lzo_compress(void *in_buf, size_t in_len, void *out_buf, size_t *out_len) { @@ -112,7 +116,7 @@ static int lzo_compress(void *in_buf, size_t in_len, void *out_buf, } #endif -#ifndef WITHOUT_ZSTD +#ifdef WITH_ZSTD static ZSTD_CCtx *zctx; static int zstd_compress(void *in_buf, size_t in_len, void *out_buf, @@ -140,7 +144,7 @@ static int no_compress(void *in_buf, size_t in_len, void *out_buf, static char *zlib_buf; -#ifndef WITHOUT_LZO +#if defined(WITH_LZO) && defined(WITH_ZLIB) static int favor_lzo_compress(void *in_buf, size_t in_len, void *out_buf, size_t *out_len, int *type) { @@ -198,22 +202,25 @@ int compress_data(void *in_buf, size_t in_len, void *out_buf, size_t *out_len, return MKFS_UBIFS_COMPR_NONE; } -#ifdef WITHOUT_LZO - { - switch (type) { -#else +#if defined(WITH_LZO) && defined(WITH_ZLIB) if (c->favor_lzo) ret = favor_lzo_compress(in_buf, in_len, out_buf, out_len, &type); else { +#else + { +#endif switch (type) { +#ifdef WITH_LZO case MKFS_UBIFS_COMPR_LZO: ret = lzo_compress(in_buf, in_len, out_buf, out_len); break; #endif +#ifdef WITH_ZLIB case MKFS_UBIFS_COMPR_ZLIB: ret = zlib_deflate(in_buf, in_len, out_buf, out_len); break; -#ifndef WITHOUT_ZSTD +#endif +#ifdef WITH_ZSTD case MKFS_UBIFS_COMPR_ZSTD: ret = zstd_compress(in_buf, in_len, out_buf, out_len); break; @@ -236,7 +243,7 @@ int compress_data(void *in_buf, size_t in_len, void *out_buf, size_t *out_len, int init_compression(void) { -#ifdef WITHOUT_LZO +#ifndef WITH_LZO lzo_mem = NULL; #else lzo_mem = malloc(LZO1X_999_MEM_COMPRESS); @@ -244,11 +251,15 @@ int init_compression(void) return -1; #endif +#ifndef WITH_ZLIB + zlib_buf = NULL; +#else zlib_buf = malloc(UBIFS_BLOCK_SIZE * WORST_COMPR_FACTOR); if (!zlib_buf) goto err; +#endif -#ifndef WITHOUT_ZSTD +#ifdef WITH_ZSTD zctx = ZSTD_createCCtx(); if (!zctx) goto err; @@ -265,7 +276,7 @@ void destroy_compression(void) { free(zlib_buf); free(lzo_mem); -#ifndef WITHOUT_ZSTD +#ifdef WITH_ZSTD ZSTD_freeCCtx(zctx); #endif if (errcnt) diff --git a/ubifs-utils/mkfs.ubifs/fscrypt.h b/ubifs-utils/mkfs.ubifs/fscrypt.h index 34b799c..ff3d326 100644 --- a/ubifs-utils/mkfs.ubifs/fscrypt.h +++ b/ubifs-utils/mkfs.ubifs/fscrypt.h @@ -131,7 +131,7 @@ static inline void free_fscrypt_context(struct fscrypt_context *fctx) { (void)fctx; - assert(0); + assert(!fctx); } static inline int encrypt_path(void **outbuf, void *data, unsigned int data_len, @@ -163,7 +163,7 @@ static inline struct fscrypt_context *inherit_fscrypt_context(struct fscrypt_con { (void)fctx; - assert(0); + assert(!fctx); return NULL; } #endif /* WITH_CRYPTO */ diff --git a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c index 8211ada..42a47f8 100644 --- a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c +++ b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c @@ -26,7 +26,7 @@ #include <crc32.h> #include "common.h" #include <sys/types.h> -#ifndef WITHOUT_XATTR +#ifdef WITH_XATTR #include <sys/xattr.h> #endif @@ -35,7 +35,7 @@ #include <selinux/label.h> #endif -#ifndef WITHOUT_ZSTD +#ifdef WITH_ZSTD #include <zstd.h> #endif @@ -56,7 +56,6 @@ #ifdef WITH_SELINUX #define XATTR_NAME_SELINUX "security.selinux" static struct selabel_handle *sehnd; -static char *secontext; #endif /** @@ -540,10 +539,12 @@ static void select_default_compr(void) return; } -#ifdef WITHOUT_LZO +#ifdef WITH_LZO + c->default_compr = UBIFS_COMPR_LZO; +#elif defined(WITH_ZLIB) c->default_compr = UBIFS_COMPR_ZLIB; #else - c->default_compr = UBIFS_COMPR_LZO; + c->default_compr = UBIFS_COMPR_NONE; #endif } @@ -682,26 +683,30 @@ static int get_options(int argc, char**argv) case 'x': if (strcmp(optarg, "none") == 0) c->default_compr = UBIFS_COMPR_NONE; +#ifdef WITH_ZLIB else if (strcmp(optarg, "zlib") == 0) c->default_compr = UBIFS_COMPR_ZLIB; -#ifndef WITHOUT_ZSTD +#endif +#ifdef WITH_ZSTD else if (strcmp(optarg, "zstd") == 0) c->default_compr = UBIFS_COMPR_ZSTD; #endif -#ifndef WITHOUT_LZO +#ifdef WITH_LZO + else if (strcmp(optarg, "lzo") == 0) + c->default_compr = UBIFS_COMPR_LZO; +#endif +#if defined(WITH_LZO) && defined(WITH_ZLIB) else if (strcmp(optarg, "favor_lzo") == 0) { c->default_compr = UBIFS_COMPR_LZO; c->favor_lzo = 1; - } else if (strcmp(optarg, "lzo") == 0) { - c->default_compr = UBIFS_COMPR_LZO; } #endif else return err_msg("bad compressor name"); break; case 'X': -#ifdef WITHOUT_LZO - return err_msg("built without LZO support"); +#if !defined(WITH_LZO) && !defined(WITH_ZLIB) + return err_msg("built without LZO or ZLIB support"); #else c->favor_percent = strtol(optarg, &endp, 0); if (*endp != '\0' || endp == optarg || @@ -733,6 +738,7 @@ static int get_options(int argc, char**argv) if (!context) return err_msg("xmalloc failed\n"); memcpy(context, optarg, context_len); + context[context_len] = '\0'; /* Make sure root directory exists */ if (stat(context, &context_st)) @@ -1288,7 +1294,7 @@ out: return ret; } -#ifdef WITHOUT_XATTR +#ifndef WITH_XATTR static inline int create_inum_attr(ino_t inum, const char *name) { (void)inum; @@ -1388,6 +1394,15 @@ static int inode_add_xattr(struct ubifs_ino_node *host_ino, continue; } +#ifdef WITH_SELINUX + /* + Ignore selinux attributes if we have a label file, they are + instead provided by inode_add_selinux_xattr. + */ + if (!strcmp(name, XATTR_NAME_SELINUX) && context && sehnd) + continue; +#endif + ret = add_xattr(host_ino, st, inum, name, attrbuf, attrsize); if (ret < 0) goto out_free; @@ -1412,12 +1427,10 @@ static int inode_add_selinux_xattr(struct ubifs_ino_node *host_ino, char *sepath = NULL; char *name; unsigned int con_size; + char *secontext; - if (!context || !sehnd) { - secontext = NULL; - con_size = 0; + if (!context || !sehnd) return 0; - } if (path_name[strlen(root)] == '/') sepath = strdup(&path_name[strlen(root)]); @@ -1594,11 +1607,11 @@ static int add_inode(struct stat *st, ino_t inum, void *data, len = UBIFS_INO_NODE_SZ + data_len; if (xattr_path) { -#ifdef WITH_SELINUX ret = inode_add_selinux_xattr(ino, xattr_path, st, inum); -#else + if (ret < 0) + return ret; + ret = inode_add_xattr(ino, xattr_path, st, inum); -#endif if (ret < 0) return ret; } @@ -1851,10 +1864,12 @@ static int add_file(const char *path_name, struct stat *st, ino_t inum, out_len = NODE_BUFFER_SIZE - UBIFS_DATA_NODE_SZ; if (c->default_compr == UBIFS_COMPR_NONE && !c->encrypted && (flags & FS_COMPR_FL)) -#ifdef WITHOUT_LZO +#ifdef WITH_LZO + use_compr = UBIFS_COMPR_LZO; +#elif defined(WITH_ZLIB) use_compr = UBIFS_COMPR_ZLIB; #else - use_compr = UBIFS_COMPR_LZO; + use_compr = UBIFS_COMPR_NONE; #endif else use_compr = c->default_compr; diff --git a/ubifs-utils/mkfs.ubifs/sign.c b/ubifs-utils/mkfs.ubifs/sign.c index b7ad7ef..7f284f8 100644 --- a/ubifs-utils/mkfs.ubifs/sign.c +++ b/ubifs-utils/mkfs.ubifs/sign.c @@ -388,7 +388,8 @@ int init_authentication(void) if (!c->hash_algo_name) return err_msg("Hash algorithm not given (--hash-algo)"); - OPENSSL_no_config(); + OPENSSL_config(NULL); + OpenSSL_add_all_algorithms(); ERR_load_crypto_strings(); diff --git a/ubifs-utils/mount.ubifs b/ubifs-utils/mount.ubifs new file mode 100755 index 0000000..b94ddc5 --- /dev/null +++ b/ubifs-utils/mount.ubifs @@ -0,0 +1,101 @@ +#!/bin/sh + +# This script should be installed as /sbin/mount.ubifs. The benefit is that an +# fstab entry like: +# +# mtd=mtddev:home /home ubifs defaults 0 0 +# +# results in the ubi contained in the mtd named "mtddev" to be attached (if not +# already done) and then the volume named "home" being mounted to /home. + +# This is called by mount with the following options: +# /sbin/mount.ubifs spec dir [-sfnv] [-N namespace] [-o options] [-t type.subtype] + +spec="$1" +shift + +mtdname2num() { + local name + + name="$1" + + for d in $(find /sys/class/mtd/ -regex '.*/mtd[0-9]*'); do + case "$d" in + *ro) + continue + ;; + esac + + if test "$name" = "$(cat "$d/name")"; then + local dev mtdnum + + dev="$(basename "$d")" + mtdnum="${dev#mtd}" + echo "$mtdnum" + return + fi + done + + return 1 +} + +mtdnum2ubi() { + local mtdnum + + mtdnum="$1" + + for d in $(find /sys/class/ubi/ -regex '.*/ubi[0-9]*'); do + case "$d" in + *_[0-9]*) + continue + ;; + esac + + if test "$mtdnum" = "$(cat "$d/mtd_num")"; then + local ubi + + ubi="$(basename "$d")" + echo "$ubi" + return; + fi + done + + return 1 +} + +mtdnum2ubi_autoattach() { + local mtdnum ubi + + mtdnum="$1" + + ubi="$(mtdnum2ubi "$mtdnum")" && { echo "$ubi"; return; } + + # ubiattach might fail with "mtdX is already attached to ubiY" if there + # is more than one mount to do in the same mtd partition. So ignore errors. + ubiattach -m "$mtdnum" >&2 || true + + mtdnum2ubi "$mtdnum" +} + +case "$spec" in + mtd=*:*) + spec="${spec#mtd=}" + mtd="${spec%:*}" + rspec="${spec#*:}" + + mtdnum="$(mtdname2num "$mtd")" || { + echo "Failed to find mtdnum for mtd \"$mtd\"" + exit 1 + } + + ubi="$(mtdnum2ubi_autoattach "$mtdnum")" || { + echo "Failed to find ubi for mtd \"$mtd\"" + exit 1 + } + + spec="$ubi:$rspec" + + ;; +esac + +/bin/mount -i -t ubifs "$spec" "$@" |