#!/usr/bin/perl
## @file biosupgrade
# @brief LinuxBase tool for upgrading BIOS ROM
# @details Use the flashrom tool for BIOS upgrade
# @param -h, --help - print the help message and exit
# @param -r, --read <image> - read BIOS ROM contents and save it into the given file
# @param -w, --write <image> - write image file into BIOS ROM
# @param -c, --check <image> - check image file
# @param --bios-version - print current BIOS version
# @author Vladimir Ivanov
# @date  March 2009
# @version 1.00 - Initial version
# @note Applicable for CPPM-v1 machines only

=pod

=head1 NAME

B<biosupgrade> -  upgrade CPPM BIOS

=head1 SYNOPSIS

 biosupgrade -r|--read  <bios image file>
 biosupgrade -w|--write <bios image file>
 biosupgrade -c|--check <bios image file>
 biosupgrade --bios-version
 biosupgrade -h|--help

=head1 DESCRIPTION

B<biosupgrade> tool 

=head1 OPTIONS

=over 12

=item B<-r|--read>

Read BIOS ROM contents and save them into the given file.

=item B<-w|--write>

Write image file into BIOS ROM.

=item B<-c|--check>

Check image file.

=item B<--bios-version>

Print current BIOS version

=item B<-h|--help>

Print this help message and exit.

=back

=head1 CAVEATS

Applicable for CPPM-v1 machines only.

=head1 SEE ALSO

=cut

use lib "/opt/nortel/base/lib";

use warnings;
use strict;
use common_variables;
use common_functions;
use CommonInstallUtils;
use bios_upgrade;
use hwRecognition;
use baselog;

## @skipline
sub biosupgrade();
## @skipline
sub bios_version();

exit(biosupgrade());

###############################################################################
## @fn biosupgrade()
# @brief Main entry point of the command
# @param
# @return $OK - Ok
# @return $ERROR - an error occurred.
sub biosupgrade()
{
   my ($action, $image) = @ARGV;

   if (defined $action and ($action eq '-h' or $action =~ /^-{1,2}help$/))
   {
      print_pod_usage();
      return $OK;
   }
   if (not is_cppm())
   {
      baselog($LOG_WARNING, "Running on unsupported platform.");
      print "\nBIOS ROM upgrade is applicable for CPPM only. Exiting... \n";
      return $ERROR;
   } ## end if (not is_cppm())

   my $retval = $OK;
   my $semmgr = undef;
   if (defined $action)
   {

      # non-interactive mode
      if ($action =~ /^-(r|w|c)$/ or $action =~ /^-{1,2}(read|write|check)$/)
      {
         if (not defined $image)
         {
            print "\nThe image file is expected as the second parameter.\n";
            return $ERROR;
         }
      } ## end if ($action =~ /^-(r|w|c)$/...

      if ($action eq '-r' or $action =~ /^-{1,2}read$/)
      {
         signals_ignore();
         $semmgr = take_biosupgrade_semaphore();
         if (defined $semmgr)
         {
            $retval = (read_image($image)) ? $OK : $ERROR;
            release_biosupgrade_semaphore($semmgr);
         }
         signals_default();
      } ## end if ($action eq '-r' or...
      elsif ($action eq '-w' or $action =~ /^-{1,2}write$/)
      {
         $retval = $ERROR;
         if (check_image($image))
         {
            signals_ignore();
            $semmgr = take_biosupgrade_semaphore();
            if (defined $semmgr)
            {
               $retval = (write_image($image)) ? $OK : $ERROR;
               release_biosupgrade_semaphore($semmgr);
            }
            signals_default();
         } ## end if (check_image($image...
      } ## end elsif ($action eq '-w' or...
      elsif ($action eq '-c' or $action =~ /^-{1,2}check$/)
      {
         $retval = (check_image($image)) ? $OK : $ERROR;
      }
      elsif ($action =~ /^-{1,2}bios-version$/)
      {
         bios_version();
         $retval = $OK;
      }
      else
      {
         print "\nInvalid option: '$action'\n";
         my $toolname = get_basename($0);
         print "Try '$toolname --help' for more information.\n";
         $retval = $ERROR;
      } ## end else [ if ($action eq '-r' or...
   } ## end if (defined $action)
   else
   {
      print "\nNo option provided.\n";
      my $toolname = get_basename($0);
      print "Try '$toolname --help' for more information.\n";
      $retval = $ERROR;
   } ## end else [ if (defined $action)

   return $retval;
} ## end sub biosupgrade()

###############################################################################
## @fn bios_version()
# @brief Print current BIOS ROM version
# @return none
sub bios_version()
{
   my $bv = get_bios_version();
   if ($bv eq "")
   {
      print "\nCannot get current BIOS ROM version.\n";
   }
   else
   {
      print "\nCurrent BIOS ROM version: $bv\n";
   }
   return;
} ## end sub bios_version()

__END__

