;============================================================================
; Name: gcd_walk.cmm
;
; Description: Dump current GCD memory map entries, symbols should be loaded
;
; Copyright (c) 2012-2016,2018 Qualcomm Technologies, Inc.
; All Rights Reserved.
; Qualcomm Technologies Proprietary and Confidential.
;
;----------------------------------------------------------------------------

;============================================================================
;
;                        EDIT HISTORY FOR MODULE
;
;
;
;  when         who     what, where, why
;  ----------   ---     -----------------------------------------------------
;  2018-07-02   yg      Revert A: to RD: get correct data through caches
;  2016-22-01   vk      Add attributes parsing
;  2015-15-04   vk      Support 64 bit addresses
;  2015-02-12   bh      Change RD: to A:
;  2015-01-27   bh      Unifying 32/64-bit scripts
;  2014-24-10   vk      Chage to Quad
;  2014-19-05   vk      Branch for 64 bit
;  2013-06-03   vk      Initial version
;============================================================================;

;
;NOTE:  symbols should be loaded
;


; ---------------------------------------------------------
; Script entry point
; ---------------------------------------------------------
  print "-----------------"
  print "+ UEFI GCD Dump +"
  print "-----------------"
  print " "
  print "Memory Types"
  print "EfiGcdMemoryTypeNonExistent           0"
  print "EfiGcdMemoryTypeReserved              1"
  print "EfiGcdMemoryTypeSystemMemory          2"
  print "EfiGcdMemoryTypeMemoryMappedIo        3"
  print "EfiGcdMemoryTypeMaximum               4"
  print " "

  local &Gcd_walk_ptr
  local &NextPtr
  local &Count
  local &PrintCount

  &Count=0
  &PrintCount=0

  local &GcdMemSpacePtr
  do ../InitOffsets.cmm
  &GcdMemSpacePtr=address.offset(mGcdMemorySpaceMap)
  ;print "&GcdMemSpacePtr"
  &Gcd_start_ptr=&GcdMemSpacePtr

  if (&ARCHState==1)
  (
    &Gcd_walk_ptr=data.long(RD:&Gcd_start_ptr)
  )
  else
  (
    &Gcd_walk_ptr=data.quad(RD:&Gcd_start_ptr)
  )

  print "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
  print "AddressRange                         Size                  Pages        Capabilities          Current Attributes    GcdMemoryType  ImgHndl               DevHandle"
  print "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"

  while (&Gcd_walk_ptr!=&Gcd_start_ptr)
  (
    gosub print_gcd &Gcd_walk_ptr+0x8+&PtrSize

    if (&ARCHState==1)
    (
      &NextPtr=data.long(RD:&Gcd_walk_ptr)
    )
    else
    (
      &NextPtr=data.quad(RD:&Gcd_walk_ptr)
    )
    &Gcd_walk_ptr=&NextPtr
  )

  print "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
  print "Count = " %Decimal &Count
  print "PrintCount = " %Decimal &PrintCount
  print "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
enddo

print_gcd:
  entry &EFI_GCD_MEMORY_SPACE_DESCRIPTOR_ptr

  local &BaseAddr
  local &EndAddr
  local &Capabilities
  local &Attributes
  local &GcdMemoryType
  local &ImageHandle
  local &DevHandle

  local &SkipGcdMemType
  &SkipGcdMemType=5
  ;&SkipGcdMemType=0

  if (&ARCHState==1)
  (
    &BaseAddr=data.long(RD:&EFI_GCD_MEMORY_SPACE_DESCRIPTOR_ptr)
    &EndAddr=data.long(RD:&EFI_GCD_MEMORY_SPACE_DESCRIPTOR_ptr+0x8)
  )
  else
  (
    &BaseAddr=data.quad(RD:&EFI_GCD_MEMORY_SPACE_DESCRIPTOR_ptr)
    &EndAddr=data.quad(RD:&EFI_GCD_MEMORY_SPACE_DESCRIPTOR_ptr+0x8)
  )

  &EndAddr=&EndAddr+1

  if (&ARCHState==1)
  (
    &Capabilities=data.long(RD:&EFI_GCD_MEMORY_SPACE_DESCRIPTOR_ptr+0x10)
    &Attributes=data.long(RD:&EFI_GCD_MEMORY_SPACE_DESCRIPTOR_ptr+0x18)
    &GcdMemoryType=data.long(RD:&EFI_GCD_MEMORY_SPACE_DESCRIPTOR_ptr+0x20)
  )
  else
  (
    &Capabilities=data.quad(RD:&EFI_GCD_MEMORY_SPACE_DESCRIPTOR_ptr+0x10)
    &Attributes=data.long(RD:&EFI_GCD_MEMORY_SPACE_DESCRIPTOR_ptr+0x18)
    &GcdMemoryType=data.quad(RD:&EFI_GCD_MEMORY_SPACE_DESCRIPTOR_ptr+0x20)
  )

  &ImageHandle=data.long(RD:&EFI_GCD_MEMORY_SPACE_DESCRIPTOR_ptr+0x28)
  &DevHandle=data.long(RD:&EFI_GCD_MEMORY_SPACE_DESCRIPTOR_ptr+0x28+&PtrSize)

  &Size=&EndAddr-&BaseAddr
  &SizeStr=FORMAT.HEX(16.,&Size)
  &Pages=(&Size/4096)
  &PagesStr=FORMAT.HEX(9.,&Pages)

  if (&GcdMemoryType!=&SkipGcdMemType)
  (
  print FORMAT.HEX(16.,&BaseAddr), "--" FORMAT.HEX(16.,(&EndAddr-1)), "   0x&SizeStr", "    &PagesStr", "    0x", FORMAT.HEX(16.,&Capabilities),  "    0x", FORMAT.HEX(16.,&Attributes),"    &GcdMemoryType" , "            0x" FORMAT.HEX(16.,&ImageHandle), "    0x", FORMAT.HEX(16.,&DevHandle)
  &PrintCount=&PrintCount+1
  )
  &Count=&Count+1
  return

enddo
