1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
  24  *
  25  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  26  * Use is subject to license terms.
  27  */
  28 
  29 
  30 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T     */
  31 /*        All Rights Reserved   */
  32 
  33 
  34 #ifndef _SYS_VTOC_H
  35 #define _SYS_VTOC_H
  36 
  37 #include <sys/dklabel.h>
  38 
  39 #ifdef  __cplusplus
  40 extern "C" {
  41 #endif
  42 
  43 /*
  44  *      Note:  the VTOC is not implemented fully, nor in the manner
  45  *      that AT&T implements it.  AT&T puts the vtoc structure
  46  *      into a sector, usually the second sector (pdsector is first).
  47  *
  48  *      Sun incorporates the tag, flag, version, and volume vtoc fields into
  49  *      its Disk Label, which already has some vtoc-equivalent fields.
  50  *      Upon reading the vtoc with read_vtoc(), the following exceptions
  51  *      occur:
  52  *              v_bootinfo [all]        returned as zero
  53  *              v_sanity                returned as VTOC_SANE
  54  *                                              if Disk Label was sane
  55  *              v_sectorsz              returned as 512
  56  *              v_reserved [all]        retunred as zero
  57  *              timestamp [all]         returned as zero
  58  *
  59  *      See  dklabel.h, read_vtoc(), and write_vtoc().
  60  */
  61 
  62 #define V_NUMPAR        NDKMAP          /* The number of partitions */
  63                                         /* (from dkio.h) */
  64 
  65 #define VTOC_SANE       0x600DDEEE      /* Indicates a sane VTOC */
  66 #define V_VERSION       0x01            /* layout version number */
  67 #define V_EXTVERSION    V_VERSION       /* extvtoc layout version number */
  68 
  69 /*
  70  * Partition identification tags
  71  */
  72 #define V_UNASSIGNED    0x00            /* unassigned partition */
  73 #define V_BOOT          0x01            /* Boot partition */
  74 #define V_ROOT          0x02            /* Root filesystem */
  75 #define V_SWAP          0x03            /* Swap filesystem */
  76 #define V_USR           0x04            /* Usr filesystem */
  77 #define V_BACKUP        0x05            /* full disk */
  78 #define V_STAND         0x06            /* Stand partition */
  79 #define V_VAR           0x07            /* Var partition */
  80 #define V_HOME          0x08            /* Home partition */
  81 #define V_ALTSCTR       0x09            /* Alternate sector partition */
  82 #define V_CACHE         0x0a            /* Cache (cachefs) partition */
  83 #define V_RESERVED      0x0b            /* SMI reserved data */
  84 
  85 /*
  86  * Partition permission flags
  87  */
  88 #define V_UNMNT         0x01            /* Unmountable partition */
  89 #define V_RONLY         0x10            /* Read only */
  90 
  91 /*
  92  * error codes for reading & writing vtoc
  93  */
  94 #define VT_ERROR        (-2)            /* errno supplies specific error */
  95 #define VT_EIO          (-3)            /* I/O error accessing vtoc */
  96 #define VT_EINVAL       (-4)            /* illegal value in vtoc or request */
  97 #define VT_ENOTSUP      (-5)            /* VTOC op. not supported */
  98 #define VT_ENOSPC       (-6)            /* requested space not found */
  99 #define VT_EOVERFLOW    (-7)            /* VTOC op. data struct limited */
 100 
 101 struct partition        {
 102         ushort_t p_tag;                 /* ID tag of partition */
 103         ushort_t p_flag;                /* permission flags */
 104         daddr_t p_start;                /* start sector no of partition */
 105         long    p_size;                 /* # of blocks in partition */
 106 };
 107 
 108 struct vtoc {
 109         unsigned long   v_bootinfo[3];  /* info needed by mboot (unsupported) */
 110         unsigned long   v_sanity;       /* to verify vtoc sanity */
 111         unsigned long   v_version;      /* layout version */
 112         char    v_volume[LEN_DKL_VVOL]; /* volume name */
 113         ushort_t        v_sectorsz;     /* sector size in bytes */
 114         ushort_t        v_nparts;       /* number of partitions */
 115         unsigned long   v_reserved[10]; /* free space */
 116         struct partition v_part[V_NUMPAR]; /* partition headers */
 117         time_t  timestamp[V_NUMPAR];    /* partition timestamp (unsupported) */
 118         char    v_asciilabel[LEN_DKL_ASCII];    /* for compatibility */
 119 };
 120 
 121 struct extpartition {
 122         ushort_t p_tag;                 /* ID tag of partition */
 123         ushort_t p_flag;                /* permission flags */
 124         ushort_t p_pad[2];
 125         diskaddr_t p_start;             /* start sector no of partition */
 126         diskaddr_t p_size;                      /* # of blocks in partition */
 127 };
 128 
 129 
 130 struct extvtoc {
 131         uint64_t        v_bootinfo[3];  /* info needed by mboot (unsupported) */
 132         uint64_t        v_sanity;       /* to verify vtoc sanity */
 133         uint64_t        v_version;      /* layout version */
 134         char    v_volume[LEN_DKL_VVOL]; /* volume name */
 135         ushort_t        v_sectorsz;     /* sector size in bytes */
 136         ushort_t        v_nparts;       /* number of partitions */
 137         ushort_t        pad[2];
 138         uint64_t        v_reserved[10];
 139         struct extpartition v_part[V_NUMPAR]; /* partition headers */
 140         uint64_t timestamp[V_NUMPAR];   /* partition timestamp (unsupported) */
 141         char    v_asciilabel[LEN_DKL_ASCII];    /* for compatibility */
 142 };
 143 
 144 #ifdef _KERNEL
 145 #define extvtoctovtoc(extv, v)                                          \
 146         {                                                               \
 147         int i;                                                          \
 148         v.v_bootinfo[0]         = (unsigned long)extv.v_bootinfo[0];    \
 149         v.v_bootinfo[1]         = (unsigned long)extv.v_bootinfo[1];    \
 150         v.v_bootinfo[2]         = (unsigned long)extv.v_bootinfo[2];    \
 151         v.v_sanity              = (unsigned long)extv.v_sanity;         \
 152         v.v_version             = (unsigned long)extv.v_version;        \
 153         bcopy(extv.v_volume, v.v_volume, LEN_DKL_VVOL);                 \
 154         v.v_sectorsz            = extv.v_sectorsz;                      \
 155         v.v_nparts              = extv.v_nparts;                        \
 156         for (i = 0; i < 10; i++)                                     \
 157                 v.v_reserved[i] = (unsigned long)extv.v_reserved[i];    \
 158         for (i = 0; i < V_NUMPAR; i++) {                             \
 159                 v.v_part[i].p_tag = extv.v_part[i].p_tag;               \
 160                 v.v_part[i].p_flag = extv.v_part[i].p_flag;             \
 161                 v.v_part[i].p_start = (daddr_t)extv.v_part[i].p_start;  \
 162                 v.v_part[i].p_size = (long)extv.v_part[i].p_size;       \
 163                 v.timestamp[i] = (time_t)extv.timestamp[i];             \
 164         }                                                               \
 165         bcopy(extv.v_asciilabel, v.v_asciilabel, LEN_DKL_ASCII);        \
 166         }
 167 
 168 #define vtoctoextvtoc(v, extv)                                          \
 169         {                                                               \
 170         int i;                                                          \
 171         extv.v_bootinfo[0]      = (uint64_t)v.v_bootinfo[0];            \
 172         extv.v_bootinfo[1]      = (uint64_t)v.v_bootinfo[1];            \
 173         extv.v_bootinfo[2]      = (uint64_t)v.v_bootinfo[2];            \
 174         extv.v_sanity           = (uint64_t)v.v_sanity;                 \
 175         extv.v_version          = (uint64_t)v.v_version;                \
 176         bcopy(v.v_volume, extv.v_volume, LEN_DKL_VVOL);                 \
 177         extv.v_sectorsz         = v.v_sectorsz;                         \
 178         extv.v_nparts           = v.v_nparts;                           \
 179         for (i = 0; i < 10; i++)                                     \
 180                 extv.v_reserved[i] = (uint64_t)v.v_reserved[i];         \
 181         for (i = 0; i < V_NUMPAR; i++) {                             \
 182                 extv.v_part[i].p_tag = v.v_part[i].p_tag;               \
 183                 extv.v_part[i].p_flag = v.v_part[i].p_flag;             \
 184                 extv.v_part[i].p_start =                                \
 185                     (diskaddr_t)(unsigned long)v.v_part[i].p_start;     \
 186                 extv.v_part[i].p_size =                                 \
 187                     (diskaddr_t)(unsigned long)v.v_part[i].p_size;      \
 188                 extv.timestamp[i] = (uint64_t)v.timestamp[i];           \
 189         }                                                               \
 190         bcopy(v.v_asciilabel, extv.v_asciilabel, LEN_DKL_ASCII);        \
 191         }
 192 #endif /* _KERNEL */
 193 
 194 #if defined(_SYSCALL32)
 195 struct partition32      {
 196         uint16_t        p_tag;          /* ID tag of partition */
 197         uint16_t        p_flag;         /* permission flags */
 198         daddr32_t       p_start;        /* start sector no of partition */
 199         int32_t         p_size;         /* # of blocks in partition */
 200 };
 201 
 202 struct vtoc32 {
 203         uint32_t        v_bootinfo[3];  /* info needed by mboot (unsupported) */
 204         uint32_t        v_sanity;       /* to verify vtoc sanity */
 205         uint32_t        v_version;      /* layout version */
 206         char    v_volume[LEN_DKL_VVOL]; /* volume name */
 207         uint16_t        v_sectorsz;     /* sector size in bytes */
 208         uint16_t        v_nparts;       /* number of partitions */
 209         uint32_t        v_reserved[10]; /* free space */
 210         struct partition32 v_part[V_NUMPAR]; /* partition headers */
 211         time32_t timestamp[V_NUMPAR];   /* partition timestamp (unsupported) */
 212         char    v_asciilabel[LEN_DKL_ASCII];    /* for compatibility */
 213 };
 214 
 215 #define vtoc32tovtoc(v32, v)                            \
 216         {                                               \
 217         int i;                                          \
 218         v.v_bootinfo[0]         = v32.v_bootinfo[0];    \
 219         v.v_bootinfo[1]         = v32.v_bootinfo[1];    \
 220         v.v_bootinfo[2]         = v32.v_bootinfo[2];    \
 221         v.v_sanity              = v32.v_sanity;         \
 222         v.v_version             = v32.v_version;        \
 223         bcopy(v32.v_volume, v.v_volume, LEN_DKL_VVOL);  \
 224         v.v_sectorsz            = v32.v_sectorsz;       \
 225         v.v_nparts              = v32.v_nparts;         \
 226         v.v_version             = v32.v_version;        \
 227         for (i = 0; i < 10; i++)                     \
 228                 v.v_reserved[i] = v32.v_reserved[i];    \
 229         for (i = 0; i < V_NUMPAR; i++) {             \
 230                 v.v_part[i].p_tag = (ushort_t)v32.v_part[i].p_tag;      \
 231                 v.v_part[i].p_flag = (ushort_t)v32.v_part[i].p_flag;    \
 232                 v.v_part[i].p_start = (unsigned)v32.v_part[i].p_start;  \
 233                 v.v_part[i].p_size = (unsigned)v32.v_part[i].p_size;    \
 234         }                                               \
 235         for (i = 0; i < V_NUMPAR; i++)                       \
 236                 v.timestamp[i] = (time_t)v32.timestamp[i];              \
 237         bcopy(v32.v_asciilabel, v.v_asciilabel, LEN_DKL_ASCII);         \
 238         }
 239 
 240 #define vtoc32toextvtoc(v32, extv)                                      \
 241         {                                                               \
 242         int i;                                                          \
 243         extv.v_bootinfo[0]              = v32.v_bootinfo[0];            \
 244         extv.v_bootinfo[1]              = v32.v_bootinfo[1];            \
 245         extv.v_bootinfo[2]              = v32.v_bootinfo[2];            \
 246         extv.v_sanity           = v32.v_sanity;                         \
 247         extv.v_version          = v32.v_version;                        \
 248         bcopy(v32.v_volume, extv.v_volume, LEN_DKL_VVOL);               \
 249         extv.v_sectorsz         = v32.v_sectorsz;                       \
 250         extv.v_nparts           = v32.v_nparts;                         \
 251         extv.v_version          = v32.v_version;                        \
 252         for (i = 0; i < 10; i++)                                     \
 253                 extv.v_reserved[i] = v32.v_reserved[i];                 \
 254         for (i = 0; i < V_NUMPAR; i++) {                             \
 255                 extv.v_part[i].p_tag = (ushort_t)v32.v_part[i].p_tag;   \
 256                 extv.v_part[i].p_flag = (ushort_t)v32.v_part[i].p_flag; \
 257                 extv.v_part[i].p_start = (diskaddr_t)v32.v_part[i].p_start; \
 258                 extv.v_part[i].p_size = (diskaddr_t)v32.v_part[i].p_size; \
 259                 extv.timestamp[i] = (time_t)v32.timestamp[i];           \
 260         }                                                               \
 261         bcopy(v32.v_asciilabel, extv.v_asciilabel, LEN_DKL_ASCII);      \
 262         }
 263 
 264 
 265 #define vtoctovtoc32(v, v32)                            \
 266         {                                               \
 267         int i;                                          \
 268         v32.v_bootinfo[0]       = v.v_bootinfo[0];      \
 269         v32.v_bootinfo[1]       = v.v_bootinfo[1];      \
 270         v32.v_bootinfo[2]       = v.v_bootinfo[2];      \
 271         v32.v_sanity            = v.v_sanity;           \
 272         v32.v_version           = v.v_version;          \
 273         bcopy(v.v_volume, v32.v_volume, LEN_DKL_VVOL);  \
 274         v32.v_sectorsz          = v.v_sectorsz;         \
 275         v32.v_nparts            = v.v_nparts;           \
 276         v32.v_version           = v.v_version;          \
 277         for (i = 0; i < 10; i++)                     \
 278                 v32.v_reserved[i] = v.v_reserved[i];    \
 279         for (i = 0; i < V_NUMPAR; i++) {             \
 280                 v32.v_part[i].p_tag = (ushort_t)v.v_part[i].p_tag;      \
 281                 v32.v_part[i].p_flag = (ushort_t)v.v_part[i].p_flag;    \
 282                 v32.v_part[i].p_start = (unsigned)v.v_part[i].p_start;  \
 283                 v32.v_part[i].p_size = (unsigned)v.v_part[i].p_size;    \
 284         }                                               \
 285         for (i = 0; i < V_NUMPAR; i++) {             \
 286                 if (v.timestamp[i] > TIME32_MAX)     \
 287                         v32.timestamp[i] = TIME32_MAX;  \
 288                 else                                    \
 289                         v32.timestamp[i] = (time32_t)v.timestamp[i];    \
 290         }                                               \
 291         bcopy(v.v_asciilabel, v32.v_asciilabel, LEN_DKL_ASCII);         \
 292         }
 293 
 294 #define extvtoctovtoc32(extv, v32)                              \
 295         {                                               \
 296         int i;                                          \
 297         v32.v_bootinfo[0]       = extv.v_bootinfo[0];   \
 298         v32.v_bootinfo[1]       = extv.v_bootinfo[1];   \
 299         v32.v_bootinfo[2]       = extv.v_bootinfo[2];   \
 300         v32.v_sanity            = extv.v_sanity;                \
 301         v32.v_version           = extv.v_version;               \
 302         bcopy(extv.v_volume, v32.v_volume, LEN_DKL_VVOL);       \
 303         v32.v_sectorsz          = extv.v_sectorsz;              \
 304         v32.v_nparts            = extv.v_nparts;                \
 305         v32.v_version           = extv.v_version;               \
 306         for (i = 0; i < 10; i++)                     \
 307                 v32.v_reserved[i] = extv.v_reserved[i]; \
 308         for (i = 0; i < V_NUMPAR; i++) {             \
 309                 v32.v_part[i].p_tag = (ushort_t)extv.v_part[i].p_tag;   \
 310                 v32.v_part[i].p_flag = (ushort_t)extv.v_part[i].p_flag; \
 311                 v32.v_part[i].p_start = (unsigned)extv.v_part[i].p_start; \
 312                 v32.v_part[i].p_size = (unsigned)extv.v_part[i].p_size; \
 313         }                                               \
 314         for (i = 0; i < V_NUMPAR; i++) {             \
 315                 if (extv.timestamp[i] > TIME32_MAX)  \
 316                         v32.timestamp[i] = TIME32_MAX;  \
 317                 else                                    \
 318                         v32.timestamp[i] = (time32_t)extv.timestamp[i]; \
 319         }                                               \
 320         bcopy(extv.v_asciilabel, v32.v_asciilabel, LEN_DKL_ASCII);      \
 321         }
 322 
 323 
 324 #endif /* _SYSCALL32 */
 325 
 326 /*
 327  * These defines are the mode parameter for the checksum routines.
 328  */
 329 #define CK_CHECKSUM     0       /* check checksum */
 330 #define CK_MAKESUM      1       /* generate checksum */
 331 
 332 extern  int     read_vtoc(int, struct vtoc *);
 333 extern  int     write_vtoc(int, struct vtoc *);
 334 extern  int     read_extvtoc(int, struct extvtoc *);
 335 extern  int     write_extvtoc(int, struct extvtoc *);
 336 
 337 #ifdef  __cplusplus
 338 }
 339 #endif
 340 
 341 #endif  /* _SYS_VTOC_H */