xref: /titanic_44/usr/src/cmd/scadm/sparc/mpxu/common/valid_srecord.c (revision 03831d35f7499c87d51205817c93e9a8d42c4bae)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * valid_srecord.c: to get and check an S-record string from a file
31  * (firmware image to be downloaded to the service processor)
32  */
33 
34 #include <stdio.h>
35 #include <string.h>
36 
37 #include "adm.h"
38 
39 
40 static unsigned long
ADM_string_to_long(char * s,int chars)41 ADM_string_to_long(char *s, int chars)
42 {
43 	unsigned long val = 0;
44 
45 	while (chars--) {
46 		val = val << 4;
47 		if ((*s >= '0') && (*s <= '9'))
48 			val += *s - '0';
49 		else if ((*s >= 'a') && (*s <= 'f'))
50 			val += *s - 'a' + 10;
51 		else if ((*s >= 'A') && (*s <= 'F'))
52 			val += *s - 'A' + 10;
53 		s++;
54 	}
55 	return (val);
56 }
57 
58 
59 int
ADM_Valid_srecord(FILE * FilePtr)60 ADM_Valid_srecord(FILE  *FilePtr)
61 {
62 	static char	Line[ADM_LINE_SIZE];
63 	char		*CurrentChar;
64 	int		SrecordLength;
65 	int		Sum;
66 
67 
68 	if (fgets(Line, ADM_LINE_SIZE, FilePtr) == NULL)
69 		return (SREC_ERR_LINE_TOO_BIG);
70 
71 	rewind(FilePtr);
72 
73 	if (strlen(Line) < 4)
74 		return (SREC_ERR_LINE_TOO_SMALL);
75 
76 	/* Check first two characters for validity */
77 	if ((Line[0] != 'S') || (Line[1] < '0') || (Line[1] > '9'))
78 		return (SREC_ERR_BAD_HEADER);
79 
80 	/* Next check the length for validity */
81 	SrecordLength = ADM_string_to_long(Line+2, 2);
82 	if (SrecordLength > ((strlen(Line) - 4) / 2))
83 		return (SREC_ERR_WRONG_LENGTH);
84 
85 	/* Check the checksum. */
86 	CurrentChar	= &Line[2];	/* Skip s-record header */
87 	SrecordLength	+= 1;		/* Include checksum */
88 	Sum		= 0;
89 	while (SrecordLength--) {
90 		Sum		+= ADM_string_to_long(CurrentChar, 2);
91 		CurrentChar	+= 2;
92 	}
93 
94 	if ((Sum & 0xFF) != 0xFF)
95 		return (SREC_ERR_BAD_CRC); /* checksum failed */
96 	else
97 		return (SREC_OK); /* checksum passed */
98 }
99