# -*- shell-script -*-

# 40ibm,vpd - Hardware database routines and variables for
#             systems with device-tree, with ibm,vpd properties.

# This file is part of the Linux lsvpd package.

# (C) Copyright IBM Corp. 2002, 2003

# Maintained by Martin Schwenke <martins@au.ibm.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    
# $Id: 40ibm,vpd,v 1.8 2004/02/25 04:41:54 martins Exp $

[ "$device_tree_device_type" = "chrp" ] || return 0

######################################################################

function get_architecture ()
{
    echo "chrp"
}

######################################################################

function list_linux_vpd_files()
{
    # Ignore stuff in the top node.

    # This could possibly be implemented by ignoring
    # list_linux_vpd_files_all and using the -mindepth option to the
    # find command, but busybox's find function doesn't have that
    # option.  It also doesn't have fgrep.
    list_linux_vpd_files_all |
    while read vpd ; do
	if [ "$vpd" != "${db_bus_dir}/./linux,vpd" ] ; then
	    echo "$vpd"
	fi
    done
}

######################################################################

# $1 = root for find
# $2 = pattern to match
# $3 = short name prefix
# $4 = description
# $5 = print location (true/false)
function dt_handle_specials ()
{
    local n=0
    local node loc_file loc vpd
    find "$1" -type d -name "$2" |
    sort |
    while read node ; do
	loc_file="${node}/ibm,loc-code"
	# Subshell strips NULs. :-)
	$5 && [ -f "$loc_file" ] && loc=$(cat "${loc_file}")
	if [ -n "$loc" ] ; then
	    vpd=$(formatvpdline "YL" "$loc")
	    vpd="${vpd}${nl}"
	fi
	maybeprintitem "${3}${n}" "$loc" "$4" "${vpd}"
	n=$(($n + 1))
    done

}

######################################################################

function do_pre_items ()
{
    maybeprintitem "sys0"       "" "System Object" ""
    maybeprintitem "sysplanar0" "" "System Planar" ""
}

function do_post_items ()
{
    local d="$db_bus_dir"

    dt_handle_specials "${d}/."    "pci@*"       "pci"     "PCI Bus"   "true"
    dt_handle_specials "${d}/."    "isa@*"       "isa"     "ISA Bus"   "true"
    dt_handle_specials "${d}/cpus" "L2-cache@*"  "L2cache" "L2 Cache"  "false"
    maybeprintitem "mem0" "" "Memory" ""
    dt_handle_specials "${d}/cpus" "PowerPC,*@*" "proc"    "Processor" "true"
}

