Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  fs/partitions/atari.c
0004  *
0005  *  Code extracted from drivers/block/genhd.c
0006  *
0007  *  Copyright (C) 1991-1998  Linus Torvalds
0008  *  Re-organised Feb 1998 Russell King
0009  */
0010 
0011 #include <linux/ctype.h>
0012 #include "check.h"
0013 #include "atari.h"
0014 
0015 /* ++guenther: this should be settable by the user ("make config")?.
0016  */
0017 #define ICD_PARTS
0018 
0019 /* check if a partition entry looks valid -- Atari format is assumed if at
0020    least one of the primary entries is ok this way */
0021 #define VALID_PARTITION(pi,hdsiz)                        \
0022     (((pi)->flg & 1) &&                              \
0023      isalnum((pi)->id[0]) && isalnum((pi)->id[1]) && isalnum((pi)->id[2]) && \
0024      be32_to_cpu((pi)->st) <= (hdsiz) &&                     \
0025      be32_to_cpu((pi)->st) + be32_to_cpu((pi)->siz) <= (hdsiz))
0026 
0027 static inline int OK_id(char *s)
0028 {
0029     return  memcmp (s, "GEM", 3) == 0 || memcmp (s, "BGM", 3) == 0 ||
0030         memcmp (s, "LNX", 3) == 0 || memcmp (s, "SWP", 3) == 0 ||
0031         memcmp (s, "RAW", 3) == 0 ;
0032 }
0033 
0034 int atari_partition(struct parsed_partitions *state)
0035 {
0036     Sector sect;
0037     struct rootsector *rs;
0038     struct partition_info *pi;
0039     u32 extensect;
0040     u32 hd_size;
0041     int slot;
0042 #ifdef ICD_PARTS
0043     int part_fmt = 0; /* 0:unknown, 1:AHDI, 2:ICD/Supra */
0044 #endif
0045 
0046     /*
0047      * ATARI partition scheme supports 512 lba only.  If this is not
0048      * the case, bail early to avoid miscalculating hd_size.
0049      */
0050     if (queue_logical_block_size(state->disk->queue) != 512)
0051         return 0;
0052 
0053     rs = read_part_sector(state, 0, &sect);
0054     if (!rs)
0055         return -1;
0056 
0057     /* Verify this is an Atari rootsector: */
0058     hd_size = get_capacity(state->disk);
0059     if (!VALID_PARTITION(&rs->part[0], hd_size) &&
0060         !VALID_PARTITION(&rs->part[1], hd_size) &&
0061         !VALID_PARTITION(&rs->part[2], hd_size) &&
0062         !VALID_PARTITION(&rs->part[3], hd_size)) {
0063         /*
0064          * if there's no valid primary partition, assume that no Atari
0065          * format partition table (there's no reliable magic or the like
0066              * :-()
0067          */
0068         put_dev_sector(sect);
0069         return 0;
0070     }
0071 
0072     pi = &rs->part[0];
0073     strlcat(state->pp_buf, " AHDI", PAGE_SIZE);
0074     for (slot = 1; pi < &rs->part[4] && slot < state->limit; slot++, pi++) {
0075         struct rootsector *xrs;
0076         Sector sect2;
0077         ulong partsect;
0078 
0079         if ( !(pi->flg & 1) )
0080             continue;
0081         /* active partition */
0082         if (memcmp (pi->id, "XGM", 3) != 0) {
0083             /* we don't care about other id's */
0084             put_partition (state, slot, be32_to_cpu(pi->st),
0085                     be32_to_cpu(pi->siz));
0086             continue;
0087         }
0088         /* extension partition */
0089 #ifdef ICD_PARTS
0090         part_fmt = 1;
0091 #endif
0092         strlcat(state->pp_buf, " XGM<", PAGE_SIZE);
0093         partsect = extensect = be32_to_cpu(pi->st);
0094         while (1) {
0095             xrs = read_part_sector(state, partsect, &sect2);
0096             if (!xrs) {
0097                 printk (" block %ld read failed\n", partsect);
0098                 put_dev_sector(sect);
0099                 return -1;
0100             }
0101 
0102             /* ++roman: sanity check: bit 0 of flg field must be set */
0103             if (!(xrs->part[0].flg & 1)) {
0104                 printk( "\nFirst sub-partition in extended partition is not valid!\n" );
0105                 put_dev_sector(sect2);
0106                 break;
0107             }
0108 
0109             put_partition(state, slot,
0110                    partsect + be32_to_cpu(xrs->part[0].st),
0111                    be32_to_cpu(xrs->part[0].siz));
0112 
0113             if (!(xrs->part[1].flg & 1)) {
0114                 /* end of linked partition list */
0115                 put_dev_sector(sect2);
0116                 break;
0117             }
0118             if (memcmp( xrs->part[1].id, "XGM", 3 ) != 0) {
0119                 printk("\nID of extended partition is not XGM!\n");
0120                 put_dev_sector(sect2);
0121                 break;
0122             }
0123 
0124             partsect = be32_to_cpu(xrs->part[1].st) + extensect;
0125             put_dev_sector(sect2);
0126             if (++slot == state->limit) {
0127                 printk( "\nMaximum number of partitions reached!\n" );
0128                 break;
0129             }
0130         }
0131         strlcat(state->pp_buf, " >", PAGE_SIZE);
0132     }
0133 #ifdef ICD_PARTS
0134     if ( part_fmt!=1 ) { /* no extended partitions -> test ICD-format */
0135         pi = &rs->icdpart[0];
0136         /* sanity check: no ICD format if first partition invalid */
0137         if (OK_id(pi->id)) {
0138             strlcat(state->pp_buf, " ICD<", PAGE_SIZE);
0139             for (; pi < &rs->icdpart[8] && slot < state->limit; slot++, pi++) {
0140                 /* accept only GEM,BGM,RAW,LNX,SWP partitions */
0141                 if (!((pi->flg & 1) && OK_id(pi->id)))
0142                     continue;
0143                 put_partition (state, slot,
0144                         be32_to_cpu(pi->st),
0145                         be32_to_cpu(pi->siz));
0146             }
0147             strlcat(state->pp_buf, " >", PAGE_SIZE);
0148         }
0149     }
0150 #endif
0151     put_dev_sector(sect);
0152 
0153     strlcat(state->pp_buf, "\n", PAGE_SIZE);
0154 
0155     return 1;
0156 }