(root)/
util-linux-2.39/
libblkid/
src/
superblocks/
ubi.c
       1  /*
       2   * Copyright (C) 2017 Rafał Miłecki <rafal@milecki.pl>
       3   *
       4   * This file may be redistributed under the terms of the
       5   * GNU Lesser General Public License.
       6   */
       7  #include <stdio.h>
       8  #include <stdlib.h>
       9  #include <unistd.h>
      10  #include <string.h>
      11  #include <stdint.h>
      12  
      13  #include "superblocks.h"
      14  #include "crc32.h"
      15  
      16  struct ubi_ec_hdr {
      17  	uint32_t	magic;
      18  	uint8_t		version;
      19  	uint8_t		padding1[3];
      20  	uint64_t	ec;
      21  	uint32_t	vid_hdr_offset;
      22  	uint32_t	data_offset;
      23  	uint32_t	image_seq;
      24  	uint8_t		padding2[32];
      25  	uint32_t	hdr_crc;
      26  } __attribute__((packed));
      27  
      28  static int ubi_verify_csum(blkid_probe pr, const struct ubi_ec_hdr *hdr)
      29  {
      30  	return blkid_probe_verify_csum(pr,
      31  			ul_crc32(~0LL, (unsigned char *) hdr,
      32  				sizeof(*hdr) - sizeof(hdr->hdr_crc)),
      33  			be32_to_cpu(hdr->hdr_crc));
      34  }
      35  
      36  static int probe_ubi(blkid_probe pr, const struct blkid_idmag *mag)
      37  {
      38  	struct ubi_ec_hdr *hdr;
      39  
      40  	hdr = blkid_probe_get_sb(pr, mag, struct ubi_ec_hdr);
      41  	if (!hdr)
      42  		return -1;
      43  
      44  	if (!ubi_verify_csum(pr, hdr))
      45  		return -1;
      46  
      47  	blkid_probe_sprintf_version(pr, "%u", hdr->version);
      48  	blkid_probe_sprintf_uuid(pr, (unsigned char *)&hdr->image_seq, 4, "%u",
      49  				 be32_to_cpu(hdr->image_seq));
      50  	return 0;
      51  }
      52  
      53  const struct blkid_idinfo ubi_idinfo =
      54  {
      55  	.name		= "ubi",
      56  	.usage		= BLKID_USAGE_RAID,
      57  	.probefunc	= probe_ubi,
      58  	.magics		=
      59  	{
      60  		{ .magic = "UBI#", .len = 4 },
      61  		{ NULL }
      62  	}
      63  };