;;; -*- 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)	;Don't try to write if nil version/revision, or
		   (fixp			;if we get error trying to read NVRAM. -ab
		     (%Nubus-Read-8-safe NVRAM-slot NVRAM-slot-offset)))
	  (Write-Current-Crash-Rec-16b CRO-LOAD-Version version)
	  (Write-Current-Crash-Rec-16b CRO-LOAD-Revision revision)
	  ;; 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-Rec 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)	;Don't try to write if time not initialized,
		   (fixp			;or if we get error trying to read NVRAM. -ab
		     (%Nubus-Read-8-safe NVRAM-slot NVRAM-slot-offset)))
	  (Write-Current-Crash-Rec CRO-BOOT-MONTH mon)
	  (Write-Current-Crash-Rec CRO-BOOT-DAY   day)
	  (Write-Current-Crash-Rec CRO-BOOT-YEAR  (\ yr 100.))
	  (Write-Current-Crash-Rec CRO-BOOT-HOUR  hrs)
	  (Write-Current-Crash-Rec CRO-BOOT-MINUTE mins) 
	  ;; Record fact that we've initialized time
	  (Write-Current-Crash-Rec 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 (SETUP-NVRAM-VARS) (SETUP-CRASH-REC-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)	;Don't try to write if time not initialized,
		   (fixp			;or if we get error trying to read NVRAM. -ab
		     (%Nubus-Read-8-safe NVRAM-slot NVRAM-Slot-Offset)))
	  (Write-Current-Crash-Rec CRO-CURRENT-MONTH mon)
	  (Write-Current-Crash-Rec CRO-CURRENT-DAY   day)
	  (Write-Current-Crash-Rec CRO-CURRENT-YEAR  (\ yr 100.))
	  (Write-Current-Crash-Rec CRO-CURRENT-HOUR  hrs)
	  (Write-Current-Crash-Rec 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.))))