summaryrefslogtreecommitdiff
path: root/usr/src/cmd/scadm/sparc/mpxu/common/send_file.c
blob: 1345b1a373df2ae89e1b72a5fea3830a29e569ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * send_file.c: to support firmware download (get fw S-records from the
 * user-specified file and send S-records string down to the service processor
 */

#include <libintl.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#include "librsc.h"
#include "adm.h"


int
ADM_Send_file(FILE  *FilePtr)
{
	static char		ADM_Line[ADM_LINE_SIZE];
	static bp_msg_t		Message;
	static struct timespec	Timeout;
	int			LinesWritten;
	int			Status;
	int			BootRetry;
	int			LastRecord;
	long			FileLength = 0L;
	long			FilePos;


	/*
	 * Determine the length of the file
	 */
	if (fseek(FilePtr, 0L, SEEK_END) == 0) {
		FileLength = ftell(FilePtr);
		rewind(FilePtr);
	}

	LinesWritten = 0;
	LastRecord   = 0;
	while ((fgets(ADM_Line, ADM_LINE_SIZE, FilePtr) != NULL) &&
	    (LastRecord == 0)) {
		if ((ADM_Line[0] == 'S') &&
		    ((ADM_Line[1] == '7') || (ADM_Line[1] == '8') ||
		    (ADM_Line[1] == '9'))) {
			LastRecord = 1;
		}

		BootRetry = ADM_BOOT_RETRY;
		while (BootRetry > 0) {
			if ((Status =
			    rsc_raw_write(ADM_Line, strlen(ADM_Line))) != 0) {
				return (Status);
			}

			/*
			 * Initialize Timeout each time just to be robust.
			 * Since this operation is not time critical, this is
			 * not a concern.
			 */
			Timeout.tv_nsec = 0;
			Timeout.tv_sec  = ADM_BOOT_LOAD_TIMEOUT;
			/* If we timeout, decrement BootRetry and try again */
			if ((Status = ADM_Boot_recv(&Message, &Timeout)) != 0) {
				/* We got a timeout */
				BootRetry = BootRetry - 1;
				continue;

			} else {

				/* we got a message back, see what it is */
				if ((Message.cmd  == BP_RSC_BOOTOK) &&
				    (Message.dat1 == 0) &&
				    (Message.dat2 == 0)) {

					LastRecord = 1;
					break;
				}

				if ((Message.cmd  != BP_RSC_BOOTACK) ||
				    (Message.dat1 != BP_DAT1_SRECORD_ACK)) {

					if (Message.dat1 ==
					    BP_DAT1_SRECORD_NAK) {
						continue;
					}
					ADM_Display_download_error(
					    (int)Message.cmd,
					    (int)Message.dat1);
					exit(-1);
				}

				/*
				 * We got a valid acknowledge, break out of
				 * loop and start to download next s-record
				 */
				break;
			}
		}

		/* See if we ever got a response */
		if (BootRetry <= 0) {
			(void) fprintf(stderr, "\n%s\n\n",
			    gettext("scadm: SC failed to respond during "
			    "download"));
			exit(-1);
		}

		LinesWritten++;
		if ((LinesWritten % 100) == 0) {
			(void) printf(".");
			(void) fflush(stdout);
		}
		if ((LinesWritten % 4000) == 0) {
			if (FileLength) {
				/* Show % progress */
				FilePos = ftell(FilePtr);
				(void) printf(" (%ld%%)",
				    (FilePos * 100) / FileLength);
			}
			(void) printf("\n");
			(void) fflush(stdout);
		}
	}
	if ((FileLength) && ((LinesWritten % 4000) != 0)) {
		/* Show final % progress (should normally be 100%) */
		FilePos = ftell(FilePtr);
		(void) printf(" (%ld%%)",
		    (FilePos * 100) / FileLength);
	}
	(void) printf("\n");

	return (0);
}


void
ADM_Display_download_error(int cmd, int dat1)
{
	if (cmd == BP_RSC_BOOTFAIL) {
		if (dat1 == BP_DAT1_REJECTED) {
			(void) fprintf(stderr, "\n%s\n\n",
			    gettext("scadm: download rejected"));

		} else if (dat1 == BP_DAT1_RANGE_ERR) {
			(void) fprintf(stderr, "\n%s\n\n",
			    gettext("scadm: download failed, "
			    "SC reported range error"));

		} else if (dat1 == BP_DAT1_VERIFY_ERR) {
			(void) fprintf(stderr, "\n%s\n\n",
			    gettext("scadm: download failed, "
			    "SC reported verify error"));

		} else if (dat1 == BP_DAT1_ERASE_ERR) {
			(void) fprintf(stderr, "\n%s\n\n",
			    gettext("scadm: download failed, "
			    "SC reported erase error"));

		} else if (dat1 == BP_DAT1_INT_WP_ERR) {
			(void) fprintf(stderr, "\n%s\n\n",
			    gettext("scadm: download failed, "
			    "SC reported int_wp error"));

		} else if (dat1 == BP_DAT1_WP_ERR) {
			(void) fprintf(stderr, "\n%s\n\n",
			    gettext("scadm: download failed, "
			    "SC reported wp error"));

		} else if (dat1 == BP_DAT1_VPP_ERR) {
			(void) fprintf(stderr, "\n%s\n\n",
			    gettext("scadm: download failed, "
			    "SC reported vpp error"));
		} else {
			(void) fprintf(stderr, "%s 0x%08x\n",
			    gettext("scadm: SC returned fatal error"), dat1);
		}
	} else if (cmd == BP_RSC_BOOTACK) {
		if (dat1 == BP_DAT1_SRECORD_ACK) {
			(void) fprintf(stderr, "\n%s\n\n",
			    gettext("scadm: download failed"));
		} else {
			(void) fprintf(stderr, "%s 0x%08x\n",
			    gettext("scadm: SC returned fatal error"), dat1);
		}
	} else {
		(void) fprintf(stderr, "%s 0x%08x:0x%08x\n",
		    gettext("scadm: SC returned unknown error"), cmd, dat1);
	}
}