SMBIOS Library
Rbu_Linux.cpp
Go to the documentation of this file.
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=c:cindent:textwidth=0:
3  *
4  * Copyright (C) 2005 Dell Inc.
5  * by Michael Brown <Michael_E_Brown@dell.com>
6  * Licensed under the Open Software License version 2.1
7  *
8  * Alternatively, you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published
10  * by the Free Software Foundation; either version 2 of the License,
11  * or (at your option) any later version.
12 
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  */
18 
19 // compat header should always be first header if including system headers
20 #include "smbios/compat.h"
21 
22 #include <iostream>
23 #include <sstream>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <time.h>
27 #include <string.h>
28 #include <errno.h>
29 
30 #include <sys/types.h>
31 #include <unistd.h>
32 
33 #include "RbuImpl.h"
34 
35 // always include last if included.
36 #include "smbios/message.h" // not needed outside of this lib. (mainly for gettext i18n)
37 
38 using namespace std;
39 
40 namespace rbu
41 {
42 const char *rbu_v0_type_file = "/proc/dell/rbu/image_type";
43 const char *rbu_v0_data_file = "/proc/dell/rbu/rbudata";
44 const char *rbu_v0_size_file = "/proc/dell/rbu/rbudatasize";
45 
46 const char *rbu_v1_mono_data_file = "/sys/firmware/rbu/rbudata";
47 const char *rbu_v1_mono_size_file = "/sys/firmware/rbu/rbudatasize";
48 const char *rbu_v1_pkt_data_file = "/sys/firmware/rbu/packetdata";
49 const char *rbu_v1_pkt_size_file = "/sys/firmware/rbu/packetdatasize";
50 
51 const char *rbu_v2_fw_data_file = "/sys/class/firmware/dell_rbu/data";
52 const char *rbu_v2_fw_load_file = "/sys/class/firmware/dell_rbu/loading";
53 const char *rbu_v2_img_type_file = "/sys/devices/platform/dell_rbu/image_type";
54 const char *rbu_v2_pkt_size_file = "/sys/devices/platform/dell_rbu/packet_size";
55 
56 const int RBU_PACKET_SIZE = 4096;
57 
59  {
60  if (!access(rbu_v1_mono_data_file, F_OK))
61  return rbu_linux_v1;
62  else if (!access(rbu_v2_img_type_file, F_OK))
63  return rbu_linux_v2;
64  else if (!access(rbu_v0_data_file, F_OK))
65  return rbu_linux_v0;
66  else
67  return rbu_unsupported;
68  }
69 
70  static FILE * writePacket(const char *fn, const char *buffer, size_t bufSize, bool openclose)
71  {
72  static FILE *data_fh = 0;
73  if(!data_fh)
74  data_fh = fopen(fn, "wb");
75 
76  if (!data_fh)
77  throw RbuDriverIOErrorImpl(strerror(errno));
78 
79  try
80  {
81  fwrite(buffer, 1, bufSize, data_fh);
82  if (ferror(data_fh))
83  throw RbuDriverIOErrorImpl(strerror(errno));
84 
85  if(openclose)
86  {
87  fclose(data_fh);
88  data_fh = 0;
89  }
90  }
91  catch(...)
92  {
93  fclose(data_fh);
94  throw;
95  }
96 
97  fflush(NULL);
98  return data_fh;
99  }
100 
101  static void pktUpdateLoop(FILE *hdr_fh, const char *packetFilename, char *buffer, size_t bufSize, bool openclose)
102  {
103  cout << "Writing RBU data (" << bufSize << "bytes/dot): ";
104 
105  fseek(hdr_fh, 0, SEEK_END);
106  size_t totalSizeBytes = ftell(hdr_fh);
107 
108  fseek(hdr_fh, 0, 0);
109  // set up packet
110  rbu_packet *pkt = reinterpret_cast<rbu_packet *>(buffer);
111  createPacket(buffer, bufSize, totalSizeBytes);
112 
113  // TODO: password support.
114  FILE *data_fh = writePacket(packetFilename, buffer, bufSize, openclose);
115  cout << ".";
116 
117  while(!feof(hdr_fh))
118  {
119  ++pkt->pktNum;
120  memset(&(pkt->pktData), 0, bufSize - sizeof(rbu_packet) + 1);
121  size_t numBytes = fread(&(pkt->pktData), 1, bufSize - sizeof(rbu_packet) + 1, hdr_fh);
122  UNREFERENCED_PARAMETER(numBytes);
123  if (ferror(hdr_fh)) // should catch all errored short reads with this
124  throw HdrFileIOErrorImpl(strerror(errno));
125 
126  checksumPacket(pkt, bufSize);
127  writePacket(packetFilename, buffer, bufSize, openclose);
128 
129  cout << ".";
130  }
131  cout << endl;
132 
133  // close data file if it is still open
134  if(data_fh)
135  {
136  fflush(NULL);
137  fclose(data_fh);
138  }
139  cout << "Done writing packet data." << endl;
140  }
141 
142  static void monoUpdateLoop(FILE *hdr_fh, FILE *data_fh)
143  {
144  fseek(hdr_fh, 0, 0);
145  const int bufSize = 4096;
146  char *buffer[bufSize];
147  cout << "Writing RBU data (" << bufSize << "bytes/dot): ";
148  while(!feof(hdr_fh))
149  {
150  memset(buffer, 0, bufSize);
151  size_t readSz = fread(buffer, 1, bufSize, hdr_fh);
152  if (ferror(hdr_fh))
153  throw HdrFileIOErrorImpl(strerror(errno));
154 
155  fwrite(buffer, 1, readSz, data_fh);
156  if (ferror(data_fh))
157  throw RbuDriverIOErrorImpl(strerror(errno));
158  cout << "." << flush;
159  }
160  cout << endl;
161  }
162 
163 
164 /****************************************
165  RBU Linux v1 functions (older driver)
166 ****************************************/
167  static void setSize(const char *fn, size_t sz)
168  {
169  FILE *size_fh = fopen(fn, "wb");
170  if (!size_fh)
171  throw RbuDriverIOErrorImpl(strerror(errno));
172 
173  ostringstream ost("");
174  ost << sz;
175  cout << "writing (" << sz << ") to file: " << fn << endl;
176  fwrite(ost.str().c_str(), 1, ost.str().length(), size_fh);
177  if (ferror(size_fh))
178  throw RbuDriverIOErrorImpl(strerror(errno));
179  fclose(size_fh);
180  size_fh = 0;
181  }
182 
183  static void doPacketUpdate_v1(FILE *hdr_fh)
184  {
185  const size_t bufSize = RBU_PACKET_SIZE;
186  char buffer[bufSize] = {0};
187 
188  // set packet size, reset mono handler
190  setSize(rbu_v1_pkt_size_file, bufSize);
191 
192  pktUpdateLoop(hdr_fh, rbu_v1_pkt_data_file, buffer, bufSize, true);
193  }
194 
195  static void doMonoUpdate_v1(FILE *hdr_fh)
196  {
197  cout << "Prep driver for data load." << endl;
198 
199  FILE *data_fh = fopen(rbu_v1_mono_data_file, "wb");
200  if (!data_fh)
201  throw RbuDriverIOErrorImpl(strerror(errno));
202 
203  fseek(hdr_fh, 0, SEEK_END);
204  size_t totalSizeBytes = ftell(hdr_fh);
205 
206  // set mono data size, reset pkt handler
208  setSize(rbu_v1_mono_size_file, totalSizeBytes);
209 
210  monoUpdateLoop(hdr_fh, data_fh);
211 
212  fclose(data_fh);
213  data_fh = 0;
214  fflush(NULL);
215 
216  cout << "BIOS staging is complete." << endl;
217  }
218 
219 /****************************************
220  RBU Linux v0 functions (2.4)
221 ****************************************/
222 
223  static void doPacketUpdate_v0(FILE *hdr_fh)
224  {
225  const size_t bufSize = RBU_PACKET_SIZE;
226  char buffer[bufSize] = {0};
227 
228  // set packet size, reset mono handler
230  setSize(rbu_v0_size_file, bufSize);
231 
232  pktUpdateLoop(hdr_fh, rbu_v0_data_file, buffer, bufSize, false);
233  }
234 
235  static void doMonoUpdate_v0(FILE *hdr_fh)
236  {
237  cout << "Prep driver for data load." << endl;
238 
239  FILE *data_fh = fopen(rbu_v0_data_file, "wb");
240  if (!data_fh)
241  throw RbuDriverIOErrorImpl(strerror(errno));
242 
243  fseek(hdr_fh, 0, SEEK_END);
244  size_t totalSizeBytes = ftell(hdr_fh);
245 
246  // set mono data size, reset pkt handler
248  setSize(rbu_v0_size_file, totalSizeBytes);
249 
250  monoUpdateLoop(hdr_fh, data_fh);
251 
252  fclose(data_fh);
253  data_fh = 0;
254  fflush(NULL);
255 
256  cout << "BIOS staging is complete." << endl;
257  }
258 
259 
260 
261 /****************************************
262  RBU Linux v2 functions (newer, included in 2.6.14+)
263 ****************************************/
264 
265  static void setPacketType(packet_type type, const char *fn=rbu_v2_img_type_file)
266  {
267  FILE *type_fh = 0;
268  type_fh = fopen(fn, "wb");
269  if (!type_fh)
270  throw RbuDriverIOErrorImpl(strerror(errno));
271 
272  switch(type)
273  {
274  case pt_mono:
275  fwrite("mono\0", 1, 5, type_fh);
276  break;
277  case pt_packet:
278  fwrite("packet\0", 1, 7, type_fh);
279  break;
280  case pt_any: /*fall thru*/
281  case pt_init: /*fall thru*/
282  default:
283  // not really a packet type, but causes driver to free its memory
284  fwrite("init\0", 1, 7, type_fh);
285  break;
286  }
287 
288  if (ferror(type_fh))
289  throw RbuDriverIOErrorImpl(strerror(errno));
290 
291  fclose(type_fh);
292  } // Step 5: set rbu cmos token
293 
294 
295  static void waitForFile(const char *fn, time_t wait)
296  {
297  time_t start = time(NULL);
298  while( access(fn, F_OK) && (time(NULL) - start < wait))
299  /*nothing*/;
300  }
301 
302  static void setLoadValue(char val)
303  {
304  FILE *load_fh = 0;
305 
307 
308  load_fh = fopen(rbu_v2_fw_load_file, "wb");
309  if (!load_fh)
310  throw RbuDriverIOErrorImpl(strerror(errno));
311 
312  fwrite(&val, 1, 1, load_fh);
313  if (ferror(load_fh))
314  throw RbuDriverIOErrorImpl(strerror(errno));
315  fclose(load_fh);
316  fflush(NULL);
317  }
318 
319  static void doPacketUpdate_v2(FILE *hdr_fh)
320  {
321  const size_t bufSize = RBU_PACKET_SIZE;
322  char buffer[bufSize] = {0};
323 
324  setSize(rbu_v2_pkt_size_file, bufSize);
325  setLoadValue('1');
326  pktUpdateLoop(hdr_fh, rbu_v2_fw_data_file, buffer, bufSize, false);
327  setLoadValue('0');
328  }
329 
330  static void doMonoUpdate_v2(FILE *hdr_fh)
331  {
332  cout << "Prep driver for data load." << endl;
333  setLoadValue('1');
334 
335  FILE *data_fh = fopen(rbu_v2_fw_data_file, "wb");
336  if (!data_fh)
337  throw RbuDriverIOErrorImpl(strerror(errno));
338 
339  monoUpdateLoop(hdr_fh, data_fh);
340 
341  fclose(data_fh);
342  data_fh = 0;
343  fflush(NULL);
344 
345  cout << "Notify driver data is finished." << endl;
346  setLoadValue('0');
347  }
348 
349 
350 
351 /*****************************************************************************
352 ******************************************************************************
353 
354 main entry points for this module.
355 
356 ******************************************************************************
357 *****************************************************************************/
358 
359  void dellBiosUpdate(const IRbuHdr &hdr, packet_type force_type)
360  {
361  FILE *hdr_fh = hdr.getFh();
362  fseek(hdr_fh, 0, 0);
363 
364  bool forced=false;
365 
366  // TODO: verify that it is a HDR file
367  // TODO: checksum HDR file
368 
369  packet_type supported_pt = getSupportedPacketType();
370  cout << "Supported RBU type for this system: (MONOLITHIC"
371  << (supported_pt == pt_packet ? ", PACKET" : "")
372  << ")"
373  << endl;
374 
375  if( force_type != pt_any )
376  {
377  supported_pt = force_type;
378  forced = true;
379  }
380 
381  driver_type dt = getDriverType();
382 
383  if (dt == rbu_linux_v2)
384  {
385  // initialize RBU driver.
386  cout << "Using RBU v2 driver. Initializing Driver. " << endl;
388 
389  // set packet/mono type
390  cout << "Setting RBU type in v2 driver to: "
391  << (supported_pt == pt_packet ? "PACKET" : "")
392  << (supported_pt == pt_mono ? "MONOLITHIC" : "")
393  << (forced ? " (FORCED) ": "" )
394  << endl;
395  setPacketType(supported_pt, rbu_v2_img_type_file);
396 
397  switch(supported_pt)
398  {
399  case pt_packet:
400  doPacketUpdate_v2(hdr_fh);
401  break;
402  case pt_mono:
403  doMonoUpdate_v2(hdr_fh);
404  break;
405  default:
406  break;
407  }
408  }
409  else if(dt == rbu_linux_v1)
410  {
411  cout << "Using RBU v1 method: "
412  << (supported_pt == pt_packet ? "PACKET" : "")
413  << (supported_pt == pt_mono ? "MONOLITHIC" : "")
414  << (forced ? " (FORCED) ": "" )
415  << endl;
416 
417  switch(supported_pt)
418  {
419  case pt_packet:
420  doPacketUpdate_v1(hdr_fh);
421  break;
422  case pt_mono:
423  doMonoUpdate_v1(hdr_fh);
424  break;
425  default:
426  break;
427  }
428  }
429  else if(dt == rbu_linux_v0)
430  {
431  cout << "Using RBU v0 driver. Initializing Driver. " << endl;
433 
434  cout << "Setting RBU type in v0 driver to: "
435  << (supported_pt == pt_packet ? "PACKET" : "")
436  << (supported_pt == pt_mono ? "MONOLITHIC" : "")
437  << (forced ? " (FORCED) ": "" )
438  << endl;
439  setPacketType(supported_pt, rbu_v0_type_file);
440 
441  switch(supported_pt)
442  {
443  case pt_packet:
444  doPacketUpdate_v0(hdr_fh);
445  break;
446  case pt_mono:
447  doMonoUpdate_v0(hdr_fh);
448  break;
449  default:
450  break;
451  }
452  }
453  else
454  {
455  throw RbuNotSupportedImpl("Could not open Dell RBU driver.");
456  }
457 
458  cout << "Activate CMOS bit to notify BIOS that update is ready on next boot." << endl;
460 
461  cout << "Update staged sucessfully. BIOS update will occur on next reboot." << endl;
462  }
463 
465  {
466  // FOR LOAD CANCEL:
467  // Step 1: always unset CMOS first
468  cout << "Cancel BIOS CMOS notification bit." << endl;
469  cancelRbuToken();
470 
471  driver_type dt = getDriverType();
472  switch(dt)
473  {
474  case rbu_linux_v2:
475  // Step 2: make sure firmware class doesn't think we are loading
476  cout << "Free kernel driver memory." << endl;
477  setLoadValue('0');
478 
479  // Step 3: tell the dell_rbu driver to free its allocated memory
480  cout << "Re-initialize driver for next user." << endl;
482  break;
483 
484  case rbu_linux_v1:
485  // Step 2: Free monolithic, if present
486  cout << "Re-initialize driver for next user." << endl;
489  fflush(NULL);
490  break;
491 
492  case rbu_linux_v0:
493  // Step 2: Free monolithic, if present
494  cout << "Re-initialize driver for next user." << endl;
497  fflush(NULL);
498  break;
499 
500 
501  default:
502  cout << "Could not determine RBU driver present, skipping." << endl;
503  break;
504  }
505  }
506 }
507