def main(): """Demonstrates FirmwareUpdater usage.""" updater = hammerd_api.FirmwareUpdater(0x18d1, 0x503c, 1, '1.1') public_key = (ctypes.c_ubyte * PUBLIC_KEY_SIZE)() # Load EC image. with open('/lib/firmware/hammer.fw', 'rb') as f: ec_image = f.read() updater.LoadEcImage(ec_image) print('Connect to base EC.') updater.TryConnectUsb() updater.SendFirstPdu() updater.SendDone() print('EC information:') print('PDU Response: %s' % updater.GetFirstResponsePdu().contents) print('RO: %s' % updater.GetSectionVersion(hammerd_api.SectionName.RO)) print('RW: %s' % updater.GetSectionVersion(hammerd_api.SectionName.RW)) print('Assume EC already in RW, send pairing challenge.') pair_manager = hammerd_api.PairManager() challenge_status = pair_manager.PairChallenge(updater.object, public_key) print('Challenge status: %d' % challenge_status) print('Jump back to RO.') updater.SendSubcommand(hammerd_api.UpdateExtraCommand.ImmediateReset) updater.CloseUsb() print('Inject all-zero entropy.') time.sleep(0.5) updater.TryConnectUsb() updater.SendSubcommand(hammerd_api.UpdateExtraCommand.StayInRO) updater.InjectEntropyWithPayload('\x00' * hammerd_api.ENTROPY_SIZE) updater.CloseUsb()
def main(): parser = argparse.ArgumentParser() parser.add_argument('field', type=str, nargs='?', help='information field') args = parser.parse_args() hammerd_args = GetHammerdArguments() updater = hammerd_api.FirmwareUpdater(int(hammerd_args['VENDOR_ID']), int(hammerd_args['PRODUCT_ID']), int(hammerd_args['USB_BUS']), int(hammerd_args['USB_PORT'])) with open(hammerd_args['EC_IMAGE_PATH'], 'rb') as f: ec_image = f.read() updater.LoadEcImage(ec_image) updater.TryConnectUsb() updater.SendFirstPdu() updater.SendDone() pdu_resp = updater.GetFirstResponsePdu().contents wp_status = (pdu_resp.flash_protection & EC_FLASH_PROTECT_GPIO_ASSERTED) > 0 wp_all = pdu_resp.flash_protection == FLASH_PROTECT_ALL touchpad_info = hammerd_api.TouchpadInfo() updater.SendSubcommandReceiveResponse( hammerd_api.UpdateExtraCommand.TouchpadInfo, "", ctypes.pointer(touchpad_info), ctypes.sizeof(touchpad_info)) # Do a pairing challenge, which will check that entropy has been injected pair_manager = hammerd_api.PairManager() challenge_status = pair_manager.PairChallenge(updater.object, None) # Print the base information. info = collections.OrderedDict() info['ro_version'] = updater.GetSectionVersion(hammerd_api.SectionName.RO) info['rw_version'] = updater.GetSectionVersion(hammerd_api.SectionName.RW) info['key_version'] = pdu_resp.key_version info['wp_screw'] = str(wp_status) info['wp_all'] = str(wp_all) info['challenge_status'] = hammerd_api.ChallengeStatus.ToStr( challenge_status) info['touchpad_id'] = '%d.0' % touchpad_info.id info['touchpad_pid'] = hex(touchpad_info.vendor) info['touchpad_fw_version'] = '%d.0' % touchpad_info.fw_version info['touchpad_fw_checksum'] = hex(touchpad_info.fw_checksum) if args.field is None: print(' '.join('%s="%s"' % (key, val) for key, val in info.items())) elif args.field in info: print(info[args.field]) else: print('Invalid args.field: "%s", should be one of %s' % (args.field, ', '.join(info.keys()))) exit(1)
def main(argv): if argv: sys.exit('Test takes no args!') updater = hammerd_api.FirmwareUpdater(common.BASE_VENDOR_ID, common.BASE_PRODUCT_ID, common.BASE_USB_PATH) public_key_first = (ctypes.c_ubyte * PUBLIC_KEY_SIZE)() # Load EC image. with open(common.IMAGE, 'rb') as f: ec_image = f.read() updater.LoadEcImage(ec_image) common.disable_hammerd() print('Connect to base EC.') common.connect_usb(updater) print('EC information:') print('PDU Response: %s' % updater.GetFirstResponsePdu().contents) print('RO: %s' % updater.GetSectionVersion(hammerd_api.SectionName.RO)) print('RW: %s' % updater.GetSectionVersion(hammerd_api.SectionName.RW)) print('Current section : %s' % updater.CurrentSection()) assert updater.CurrentSection() == 1, 'Running section should be 1 (RW)' # Sends 'Need to inject entropy' message if base is new pair_manager = hammerd_api.PairManager() challenge_status = pair_manager.PairChallenge(updater.object, public_key_first) print('Challenge status: %d' % challenge_status) # assert challenge_status == 9, 'Need to inject the entropy' for iteratn in range(INJECTION_RUNS): print('Jumping back to RO to inject entropy. Iteratn: %d' % (iteratn + 1)) updater.SendSubcommand(hammerd_api.UpdateExtraCommand.UnlockRollback) updater.SendSubcommand(hammerd_api.UpdateExtraCommand.ImmediateReset) updater.CloseUsb() time.sleep(0.5) updater.TryConnectUsb() updater.SendSubcommand(hammerd_api.UpdateExtraCommand.StayInRO) # Wait for RO to run, else SendFirstPdu() picks up RW time.sleep(1) # Verify that we're in RO assert updater.SendFirstPdu() is True, 'Error sending first PDU' updater.SendDone() assert updater.CurrentSection( ) == 0, 'Not in RO: Cannot inject entropy' print('Inject entropy and sys jump to RW') updater.InjectEntropyWithPayload(b'\x87' * hammerd_api.ENTROPY_SIZE) updater.SendSubcommand(hammerd_api.UpdateExtraCommand.ImmediateReset) updater.CloseUsb() time.sleep(0.5) common.connect_usb(updater) updater.SendSubcommand(hammerd_api.UpdateExtraCommand.JumpToRW) # Wait for RW to run time.sleep(1) updater.CloseUsb() time.sleep(0.5) # Jump to RW resets the base. Need to reconnect common.connect_usb(updater) print('PDU Response: %s' % updater.GetFirstResponsePdu().contents) # Check that RW is running assert updater.SendFirstPdu() is True, 'Error sending first PDU' updater.SendDone() print('Current running section after jumping to RW: %s' % updater.CurrentSection()) assert updater.CurrentSection( ) == 1, 'Running section should be 1 (RW)' # Autheticator should match for each pairing run for i in range(PAIRING_RUNS): public_key = (ctypes.c_ubyte * PUBLIC_KEY_SIZE)() pair_manager = hammerd_api.PairManager() challenge_status = pair_manager.PairChallenge( updater.object, public_key) print('Challenge status: %d' % challenge_status) assert challenge_status == 0, 'Pairing challenge failed!' if i == 0: same = True for j in range(0, PUBLIC_KEY_SIZE - 1): if public_key_first[j] != public_key[j]: same = False assert not same, 'The key did not change after entropy injection!' public_key_first = public_key else: for j in range(0, PUBLIC_KEY_SIZE - 1): assert public_key_first[j] == public_key[j], 'Key changed!' # Reset the base common.sim_disconnect_connect(updater)