#!/usr/bin/env python3
# =============================================================================
#  get_macbook_ramcfg
# =============================================================================
#
#  Decode Apple MacBook RAM configuration (RAMCFG) from Intel chipset GPIO
#  registers using output from `inteltool -g`.
#
#  This script maps Apple’s undocumented GPIO-based RAM configuration
#  encoding to concrete memory vendor, size, and speed identifiers for
#  specific MacBook models.
#
#  USAGE
#      sudo inteltool -g | get_macbook_ramcfg -m <MODEL>
#
#  SUPPORTED MODELS
#      mbp101  - MacBook Pro 10,1
#      mba52   - MacBook Air 5,2
#      mba42   - MacBook Air 4,2
#
#  NOTES
#      - Reads GPIO68–GPIO71 from the GPIO_LVL3 register
#      - Reverses bit order to match Apple’s wiring
#      - Uses RAMCFG value as an index into model-specific lookup tables
#
#  CREDITS
#      Original implementation by ch1p (https://ch1p.io/)
#
#  LICENSE
#      BSD-2-Clause
#
#  EXTENSIONS & MAINTENANCE
#      Extended and maintained by Kris Yotam
#
# =============================================================================

#!/usr/bin/env python3
import sys
import argparse


ramconfigs = {
    'mba42': (
        '2b_hynix',
        None,
        '4g_hynix',
        None,
        '2g_samsung',
        None,
        '4g_samsung',
        None,
        '2g_micron',
        None,
        None,
        None,
        None,
        None,
        '4g_elpida',
    ),
    'mba52': (
        '4g_hynix',
        '4g_micron',
        '8g_hynix',
        None,
        '4g_samsung',
        None,
        '8g_samsung',
        None,
        None,
        None,
        None,
        None,
        '4g_elpida',
        None,
        '8g_elpida',
    ),
    'mbp101': (
        '4g_hynix_1600s',
        '1g_samsung_1600',
        '4g_samsung_1600s',
        '1g_hynix_1600',
        '4g_elpida_1600s',
        '2g_samsung_1600',
        '2g_samsung_1333',
        '2g_hynix_1600',
        '4g_samsung_1600',
        '4g_hynix_1600',
        '2g_elpida_1600s',
        '2g_elpida_1600',
        '4g_elpida_1600',
        '2g_samsung_1600s',
        '2g_hynix_1600s'
    )
}


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-m', '--model',
                        choices=list(ramconfigs.keys()),
                        help='MacBook model',
                        required=True)

    args = parser.parse_args()

    configs = ramconfigs[args.model]
    
    reg = None
    for line in sys.stdin:
        line = line.strip()
        if not line.endswith('(GPIO_LVL3)'):
            continue

        reg = int(line.split(' ')[1], 16)
        break

    if reg is None:
        raise Exceptions("failed to parse gpio registers")

    # GPIO68..GPIO71
    ramcfg = (reg >> 4) & 0xf

    # reverse bit order
    ramcfg = int('{:04b}'.format(ramcfg)[::-1], 2)

    if ramcfg >= len(configs) or configs[ramcfg] is None:
        print("unsupported memory configuration %d" % ramcfg)
    else:
        print("%s (RAMCFG=%01x)" % (configs[ramcfg], ramcfg))
        

if __name__ == '__main__':
    main()
