;;; -*- Mode:Lisp; Readtable:CL; Package:USER; Base:10; Patch-File:T -*-
;;; Patch file for System version 124.23
;;; Reason:
;;;  Give user warning and prescriptive notification when booting as AMNESIA.
;;;  
;;;  To do this well, defined CHAOS:AMNESIA-ADDRESS, the system constant specifying
;;;  the Chaos address for an amnesiac system.  Also defined (CHAOS:AMNESIA-P) to return
;;;  whether the system is AMNESIA.
;;;  
;;;  Now, when the system determines its own address and host information (e.g., cold-boot),
;;;  the user will get a warning notification with suggestions on how to get site information.
;;;  
;;;    -Keith and PLD
;;; Written 3-Jun-88 17:15:36 by keith at site Gigamos Cambridge
;;; while running on Breaking Glass from band 2
;;; with Experimental System 124.21, Experimental Local-File 74.1, Experimental File-Server 23.1, Experimental Unix-Interface 12.0, Experimental ZMail 72.0, Experimental Tape 23.6, Experimental Lambda-Diag 16.1, microcode 1756, SDU Boot Tape 3.14, SDU ROM 103.



; From modified file DJ: L.NETWORK.CHAOS; CHSNCP.LISP#408 at 3-Jun-88 17:15:42
#10R CHAOS#: 
(COMPILER-LET ((*PACKAGE* (PKG-FIND-PACKAGE "CHAOS")))
  (COMPILER::PATCH-SOURCE-FILE "SYS: NETWORK; CHAOS; CHSNCP  "

(defconstant amnesia-address #o3412 "Chaosnet address for local host that can't find its own address")

))

; From modified file DJ: L.NETWORK.CHAOS; CHSNCP.LISP#408 at 3-Jun-88 17:15:46
#10R CHAOS#: 
(COMPILER-LET ((*PACKAGE* (PKG-FIND-PACKAGE "CHAOS")))
  (COMPILER::PATCH-SOURCE-FILE "SYS: NETWORK; CHAOS; CHSNCP  "

(defun amnesia-p ()
  (or (not (boundp 'my-address))
      (null my-address)
      (= my-address amnesia-address)))

))

; From modified file DJ: L.NETWORK.CHAOS; CHSNCP.LISP#408 at 3-Jun-88 17:15:54
#10R CHAOS#: 
(COMPILER-LET ((*PACKAGE* (PKG-FIND-PACKAGE "CHAOS")))
  (COMPILER::PATCH-SOURCE-FILE "SYS: NETWORK; CHAOS; CHSNCP  "

(defun setup-my-address ()
  (let ((old-address (and (boundp 'my-address) my-address)))
    (select-processor
      (:cadr
	(setq my-address (%unibus-read my-number-register)))	;Full address of this host.
      ((:lambda :explorer)
       (let ((names-for-this-machine (multiple-value-list (si:get-pack-name)))
	     my-name)
	 (cond ((and (= si:processor-type-code si:lambda-type-code)
		     (variable-boundp si:*my-proc-number*))
		(setq my-name (nth si:*my-proc-number* names-for-this-machine)))
	       (t
		(setq my-name (car names-for-this-machine))))
	 (without-interrupts
	   (setq my-address amnesia-address)	; Flag that address is bad
	   (when (stringp my-name)
	     (let ((host (si:parse-host my-name t nil)))
	       (when host
		 (setq my-address (send host :network-address :chaos))))))
	 )))
    (cond ((eql old-address my-address))	;Chaos address unchanged
	  ((null old-address))			;Didn't used to have address but now we do
	  ((null my-address)			;Used to have an Chaos address but now we don't -- disable Chaos
	   (when *chaos-stream*
	     (send *chaos-stream* :close)))
	  (t
	   ;;We used to have an Chaos address and we still do -- but it has changed.  We must change the
	   ;;addresses in the Network Interfaces so that ARP will work right
	   (dolist (ni net:*network-interfaces*)
	     (net:delete-from-alist :chaos (net:ni-address-alist ni))
	     (push (list :chaos my-address) (net:ni-address-alist ni)))
	   (when *chaos-stream*
	     (setf (chaos-addresses *chaos-stream*)
		   (substitute my-address old-address (chaos-addresses *chaos-stream*)))))
	  ))
  (setq my-subnet (chaos-subnet-number-from-address my-address))	;Subnet of this host.
  (let ((existing-host (si:get-host-from-address my-address :chaos)))
    (setq si:local-host
	  (if (and existing-host (eq (send existing-host :system-type) :lispm))
	      existing-host
	    (si:make-unnamed-host :lispm `(:chaos (,my-address))))))
  (when (amnesia-p) 
    (tv:careful-notify nil t
     "This host does not know its Chaos address; it needs updated site information.~
      ~:[~%Try:  (UPDATE-SITE-CONFIGURATION-INFO).~;~
      ~%But loading site files from the system host will not work, because~
      ~%this host does not control an ethernet interface.~
      ~%Look at the documentation for (SI:SET-PROCESSOR-OWNING-ETHERNET).~]"
      (neq si:*my-op* si:*ethernet-hardware-controller*)))
  si:local-host)

))
