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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2002 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * valid_srecord.c: to get and check an S-record string from a file 29 * (firmware image to be downloaded to the service processor) 30 */ 31 32 #include <stdio.h> 33 #include <string.h> 34 35 #include "adm.h" 36 37 38 static unsigned long 39 ADM_string_to_long(char *s, int chars) 40 { 41 unsigned long val = 0; 42 43 while (chars--) { 44 val = val << 4; 45 if ((*s >= '0') && (*s <= '9')) 46 val += *s - '0'; 47 else if ((*s >= 'a') && (*s <= 'f')) 48 val += *s - 'a' + 10; 49 else if ((*s >= 'A') && (*s <= 'F')) 50 val += *s - 'A' + 10; 51 s++; 52 } 53 return (val); 54 } 55 56 57 int 58 ADM_Valid_srecord(FILE *FilePtr) 59 { 60 static char Line[ADM_LINE_SIZE]; 61 char *CurrentChar; 62 int SrecordLength; 63 int Sum; 64 65 66 if (fgets(Line, ADM_LINE_SIZE, FilePtr) == NULL) 67 return (SREC_ERR_LINE_TOO_BIG); 68 69 rewind(FilePtr); 70 71 if (strlen(Line) < 4) 72 return (SREC_ERR_LINE_TOO_SMALL); 73 74 /* Check first two characters for validity */ 75 if ((Line[0] != 'S') || (Line[1] < '0') || (Line[1] > '9')) 76 return (SREC_ERR_BAD_HEADER); 77 78 /* Next check the length for validity */ 79 SrecordLength = ADM_string_to_long(Line+2, 2); 80 if (SrecordLength > ((strlen(Line) - 4) / 2)) 81 return (SREC_ERR_WRONG_LENGTH); 82 83 /* Check the checksum. */ 84 CurrentChar = &Line[2]; /* Skip s-record header */ 85 SrecordLength += 1; /* Include checksum */ 86 Sum = 0; 87 while (SrecordLength--) { 88 Sum += ADM_string_to_long(CurrentChar, 2); 89 CurrentChar += 2; 90 } 91 92 if ((Sum & 0xFF) != 0xFF) 93 return (SREC_ERR_BAD_CRC); /* checksum failed */ 94 else 95 return (SREC_OK); /* checksum passed */ 96 } 97