;;; -*- Mode:Lisp; Package:SYSTEM-INTERNALS; Base:10 -*-

;;;                           RESTRICTED RIGHTS LEGEND

;;;Use, duplication, or disclosure by the Government is subject to
;;;restrictions as set forth in subdivision (b)(3)(ii) of the Rights in
;;;Technical Data and Computer Software clause at 52.227-7013.
;;;
;;;                     TEXAS INSTRUMENTS INCORPORATED.
;;;                              P.O. BOX 2909
;;;                           AUSTIN, TEXAS 78769
;;;                                 MS 2151
;;;
;;; Copyright (C) 1985, Texas Instruments Incorporated. All rights reserved.

;;;
;;; Support for filling in parts of the crash record from LISP.
;;;

(Defun Record-System-Version ()
  "Fills in system version/revision fields in NVRAM after boot.  Since this is
done in the warm initializations, record fact we've gotten this far in the
PROGRESS field also."
  (Select-Processor
    ((:cadr :lambda))
    (:explorer
      (Multiple-Value-Bind (version revision)
	  (Get-System-Version)
	(When (And (And version revision)
		   (Fixp
		     (%Nubus-Read-8-Safe NVRAM-SLOT NVRAM-SLOT-OFFSET)))
	  (Write-Current-Crash-Record-16b CRO-LOAD-VERSION version)
	  (Write-Current-Crash-Record-16b CRO-LOAD-REVISION revision)
	  (Write-Current-Crash-Record-16b CRO-UCODE-VERSION %MICROCODE-VERSION-NUMBER)
	  ;; Record fact that we're in warm initializations.  This function must be run BEFORE Record-Boot-Time
	  ;; if crash record time reporting is going to work.
	  (Write-Current-Crash-Record CRO-PROGRESS CREC-PROGRESS-WARM-INITIALIZATIONS))))))


(Defun Record-Boot-Time ()
  "Write boot time to NVRAM crash record.  Called from warm init list.  Must be called after
initialize-timebase."
  (Select-Processor
    ((:cadr :lambda))
    (:explorer
      (Multiple-Value-Bind (ignore mins hrs day mon yr) 
	  (Time:Get-Time)
	(When (And (And mins hrs day mon yr)
		   (Fixp
		     (%Nubus-Read-8-Safe NVRAM-SLOT NVRAM-SLOT-OFFSET)))
	  (Write-Current-Crash-Record CRO-BOOT-MONTH mon)
	  (Write-Current-Crash-Record CRO-BOOT-DAY   day)
	  (Write-Current-Crash-Record CRO-BOOT-YEAR  (\ yr 100.))
	  (Write-Current-Crash-Record CRO-BOOT-HOUR  hrs)
	  (Write-Current-Crash-Record CRO-BOOT-MINUTE mins) 
	  ;; Record fact that we've initialized time
	  (Write-Current-Crash-Record CRO-PROGRESS CREC-PROGRESS-TIME-INITIALIZED))))))


;;; Add these functions to warm init list.  Do NOT change the order of these two Add-Initialization's!!
(Add-Initialization "Record system version and progress in Crash Record"
		    '(Progn (Initialize-NVRAM-Vars) (Initialize-Crash-Record-Vars) (Record-System-Version)) :first)
(Add-Initialization "Record boot time and progress in Crash Record" '(Record-Boot-Time) :first)



;;;
;;; Here we define a background process that updates time field in current crash record every 5 
;;; minutes so we can know about how long the machine was up before it crashed.
;;;

(Defun Update-CREC-Time ()
  "Write current time to NVRAM crash record so that we can always know when we crashed.
Called periodically from Tm-Update process."
  (Select-Processor
    ((:cadr :lambda))
    (:explorer
      (Multiple-Value-Bind (ignore mins hrs day mon yr)
	  (Time:Get-Time)
	(When (And (And mins hrs day mon yr)
		   (Fixp
		     (%Nubus-Read-8-safe NVRAM-SLOT NVRAM-SLOT-OFFSET)))
	  (Write-Current-Crash-Record CRO-CURRENT-MONTH mon)
	  (Write-Current-Crash-Record CRO-CURRENT-DAY   day)
	  (Write-Current-Crash-Record CRO-CURRENT-YEAR  (\ yr 100.))
	  (Write-Current-Crash-Record CRO-CURRENT-HOUR  hrs)
	  (Write-Current-Crash-Record CRO-CURRENT-MINUTE mins)
	  )))))

;;; Define the Tm-Update process.  Make sure it is re-started after resets and boots.  Give it
;;; LOW priority.
(Process-Run-Function '(:name "Tm-Update" :restart-after-reset t :restart-after-boot t :priority -5)
		      #'(Lambda () (Do-Forever (Update-CREC-Time)(Sleep 300.))))