Annotation of driverkit/libDriver/label_subr.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
                      3:  *
                      4:  * @APPLE_LICENSE_HEADER_START@
                      5:  * 
                      6:  * Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
                      7:  * Reserved.  This file contains Original Code and/or Modifications of
                      8:  * Original Code as defined in and that are subject to the Apple Public
                      9:  * Source License Version 1.1 (the "License").  You may not use this file
                     10:  * except in compliance with the License.  Please obtain a copy of the
                     11:  * License at http://www.apple.com/publicsource and read it before using
                     12:  * this file.
                     13:  * 
                     14:  * The Original Code and all software distributed under the License are
                     15:  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
                     16:  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
                     17:  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
                     18:  * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT.  Please see the
                     19:  * License for the specific language governing rights and limitations
                     20:  * under the License.
                     21:  * 
                     22:  * @APPLE_LICENSE_HEADER_END@
                     23:  */
                     24: /*     Copyright (c) 1992 NeXT Computer, Inc.  All rights reserved. 
                     25:  *
                     26:  * label_subr.c - machine-independent disk label routines
                     27:  *
                     28:  * HISTORY
                     29:  * 01-Apr-92    Doug Mitchell at NeXT
                     30:  *      Created.
                     31:  */
                     32: #import <bsd/sys/param.h> 
                     33: #import <bsd/dev/disk_label.h>
                     34: #import <driverkit/disk_label.h>
                     35: #import <driverkit/diskstruct.h>
                     36: #import <architecture/byte_order.h>
                     37: 
                     38: 
                     39: /*
                     40:  * Inlines used to manipulate m68k-style disk label fields.
                     41:  */
                     42: static inline unsigned int
                     43: get_word(const void *src)
                     44: {
                     45:        return ( NXSwapBigIntToHost(*((unsigned int *)src)));
                     46: }
                     47: 
                     48: static inline void
                     49: put_word(unsigned s, void *dest)
                     50: {
                     51:        *((unsigned int *)dest) = NXSwapHostIntToBig(s);
                     52: }
                     53: 
                     54: 
                     55: static inline unsigned short
                     56: get_short(const void *src)
                     57: {
                     58:        return ( NXSwapBigShortToHost(*((unsigned short *)src)));
                     59: }
                     60: 
                     61: static inline void
                     62: put_short(unsigned short s, void *dest)
                     63: {
                     64:        *((unsigned short *)dest) = NXSwapHostShortToBig(s);
                     65: }
                     66: 
                     67: /*
                     68:  * Machine-independent version of kernel's checksum_16().
                     69:  * *wp is a raw m68k-style label.
                     70:  */
                     71: unsigned short 
                     72: checksum16(unsigned short *wp, int num_shorts)
                     73: {
                     74:        int sum1 = 0;
                     75:        int sum2;
                     76: 
                     77:        while (num_shorts--){
                     78:                sum1 += NXSwapBigShortToHost(*wp);
                     79:                wp++;
                     80:        }
                     81:        sum2 = ((sum1 & 0xffff0000) >> 16) + (sum1 & 0xffff);
                     82:        if(sum2 > 65535) {
                     83:                sum2 -= 65535;
                     84:        }
                     85:        return sum2;
                     86: }
                     87: 
                     88: /*
                     89:  * Validate an m68k-style label. Returns NULL if good, else returns pointer
                     90:  * to an error string describing what is wrong with the label.
                     91:  */
                     92: char *
                     93: check_label(char *raw_label,                   // as it came off disk
                     94:        int block_num)  
                     95: {
                     96:        unsigned short  sum;
                     97:        unsigned short  cksum;
                     98:        unsigned short  size;
                     99:        void            *dl_cksump;
                    100:        int              version;
                    101:        int             label_blkno;
                    102:        
                    103:        version = get_word(raw_label + DISK_LABEL_DL_VERSION);
                    104:        if(version == DL_V1 || version == DL_V2) {
                    105:                size = SIZEOF_DISK_LABEL_T;
                    106:                dl_cksump = raw_label + DISK_LABEL_DL_CHECKSUM;
                    107:        } 
                    108:        else if(version == DL_V3) {
                    109:                size = SIZEOF_DISK_LABEL_T - SIZEOF_DL_UN_T;
                    110:                dl_cksump = raw_label + DISK_LABEL_DL_UN;
                    111:        } 
                    112:        else {
                    113:                return "Bad disk label magic number";
                    114:        }
                    115:        label_blkno = get_word(raw_label + DISK_LABEL_DL_LABEL_BLKNO);
                    116:        if(label_blkno != block_num) {
                    117:                return "Label in wrong location";
                    118:        }
                    119:                
                    120:        /*
                    121:         * Block number in label has to be 0 for checksum calculation.
                    122:         */
                    123:        put_word(0, raw_label + DISK_LABEL_DL_LABEL_BLKNO);
                    124:        cksum = get_short(dl_cksump);
                    125:        put_short(0, dl_cksump);
                    126:        sum = checksum16((unsigned short *)raw_label, size >> 1);
                    127:        if (sum != cksum) {
                    128:                return "Label checksum error";
                    129:        }
                    130:        
                    131:        /*
                    132:         * Success. Restore block number.
                    133:         */
                    134:        put_word(block_num, raw_label + DISK_LABEL_DL_LABEL_BLKNO);
                    135:        put_short(cksum, dl_cksump);
                    136:        return NULL;
                    137: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.