Exemplo n.º 1
0
// Go into flashing mode
int flash_start_flashing(int enable_bl_writes)
{
	uint32_t state;

	if (enter_bootloader() < 0)
		return -1;

	if (get_device_state(&state) < 0)
		return -1;

	if (state & DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH) {
		// This command is stupid. Why the heck does it care which area we're
		// flashing, as long as it's not the bootloader area? The mind boggles.
		UsbCommand c = {CMD_START_FLASH};

		if (enable_bl_writes) {
			c.arg[0] = FLASH_START;
			c.arg[1] = FLASH_END;
			c.arg[2] = START_FLASH_MAGIC;
		} else {
			c.arg[0] = BOOTLOADER_END;
			c.arg[1] = FLASH_END;
			c.arg[2] = 0;
		}
		SendCommand(&c);
		return wait_for_ack();
	} else {
		fprintf(stderr, "Note: Your bootloader does not understand the new START_FLASH command\n");
		fprintf(stderr, "      It is recommended that you update your bootloader\n\n");
	}

	return 0;
}
Exemplo n.º 2
0
enum DEVICE_STATE personalize (int fd, enum DEVICE_STATE goal,
                               struct key_container *keys)
{
  enum DEVICE_STATE state = get_device_state (fd);

  if (state >= goal)
    return state;

  if (set_config_zone (fd) && lock_config_zone (fd, state))
    {
      state = STATE_INITIALIZED;
      assert (get_device_state (fd) == state);

      struct octet_buffer otp_zone;
      if (set_otp_zone (fd, &otp_zone))
        {
          struct octet_buffer data_zone;
          if (write_keys (fd, keys, &data_zone))
            {
              uint16_t crc = crc_data_otp_zone (data_zone, otp_zone);

              if (lock (fd, DATA_ZONE, crc))
                {
                  state = STATE_PERSONALIZED;
                  assert (get_device_state (fd) == state);
                }

              free_octet_buffer (data_zone);
            }

          free_octet_buffer (otp_zone);
        }
    }

  return state;

}
Exemplo n.º 3
0
// Enter the bootloader to be able to start flashing
static int enter_bootloader(void)
{
	uint32_t state;

	if (get_device_state(&state) < 0)
		return -1;

	if (state & DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM) {
		/* Already in flash state, we're done. */
		return 0;
	}

	if (state & DEVICE_INFO_FLAG_CURRENT_MODE_OS) {
		fprintf(stderr,"Entering bootloader...\n");
		UsbCommand c;
		memset(&c, 0, sizeof (c));

		if ((state & DEVICE_INFO_FLAG_BOOTROM_PRESENT)
			&& (state & DEVICE_INFO_FLAG_OSIMAGE_PRESENT))
		{
			// New style handover: Send CMD_START_FLASH, which will reset the board
			// and enter the bootrom on the next boot.
			c.cmd = CMD_START_FLASH;
			SendCommand(&c);
			fprintf(stderr,"(Press and release the button only to abort)\n");
		} else {
			// Old style handover: Ask the user to press the button, then reset the board
			c.cmd = CMD_HARDWARE_RESET;
			SendCommand(&c);
			fprintf(stderr,"Press and hold down button NOW if your bootloader requires it.\n");
		}
		fprintf(stderr,"Waiting for device to reappear on USB...");

		CloseDevice();
		sleep(1);
		while (!OpenDevice(0)) {
			sleep(1);
			fprintf(stderr, ".");
		}
		fprintf(stderr," Found.\n");

		return 0;
	}

	fprintf(stderr, "Error: Unknown bootloader mode\n");
	return -1;
}
Exemplo n.º 4
0
void power_event(int update_leds)
{
    struct device_state state;

    get_device_state(&state);

    if (state.voltage_level >= POWERUP_VOLTAGE) {
        powerup = 1;
        ALOGD("voltage ok for PU %d\n", state.voltage_level);
    }
    screen_update(state.charge_level,
            state.is_unknown || !state.is_battery_present);
    if (update_leds)
        set_battery_led(&state);

    /* When the charger is unplugged the device shall shutdown at once. */
    if (!shutdown && !state.is_plugged_into_ac && !state.is_plugged_into_usb) {
        shutdown = 1;
        alarm_cancel(screen_brightness_animation_alarm2);
        alarm_cancel(screen_brightness_animation_alarm1);
        screen_brightness_animation_alarm2(NULL);
    }
}
Exemplo n.º 5
0
int get_current_network_device()
{
	char *dev_list[30];
	int i = 0, state, type, num_of_dev = 0;

	/* Get the list of available interfaces for the given system.
	 * This function would give the object path of the devices */
	num_of_dev = get_device_list(conn, dev_list);

	/* For each given object path get the current state */

	for (i = 0; i < num_of_dev; i++) {
		state = get_device_state(conn, dev_list[i]);

		if (state == ACTIVE) {
			/* If the state is connected then get the type of the device
			 * corresponding to the object path */
			get_device_properties(conn, dev_list[i], &type);
			return type;
		}

	}
}
Exemplo n.º 6
0
int main()
{
    struct device_state old_state;
    get_device_state(&old_state);

    if (screen_init() < 0)
        goto err1;
    ev_init();
    led_init();

    sleep(3);

    /* Set battery LED, initialize image, screen brightness */
    power_event(1);
    screen_brightness_animation_start();

    while (!quit) {
        int r, delay = alarm_get_time_until_next();
        if (delay < 1) {
            alarm_process();
            continue;
        }

        r = ev_get(delay);

        /* Press below keys will wake the display and repeat the
           display cycle:
           Power key, Volume up/down key, Camera key.
           Long press Power key will reboot the device. */
        switch (r) {
        /* Power key */
        case EVENT_POWER_KEY_DOWN:
            if (powerup)
            alarm_set_relative(power_key_alarm, NULL, 1000);
            break;
        case EVENT_POWER_KEY_UP:
            alarm_cancel(power_key_alarm);
            update_screen_on_wakeup_key();
            break;
        /* Other keys */
        case EVENT_VOLUMEDOWN_KEY_DOWN:
            update_screen_on_wakeup_key2();
            break;
        case EVENT_VOLUMEUP_KEY_DOWN:
            update_screen_on_wakeup_key2();
            break;
        case EVENT_CAMERA_KEY_DOWN:
            update_screen_on_wakeup_key2();
            break;
        /* Battery events */
        case EVENT_BATTERY:
            power_event(1);
            break;
        /* Others */
        case EVENT_QUIT:
            quit = 1;
            break;
        default:
            break;
        }
    }

    led_uninit();
    ev_exit();
    screen_uninit();

    return 0;

err1:
    return -1;

}
Exemplo n.º 7
0
bool write_keys (int fd, struct key_container *keys,
                 struct octet_buffer *data_zone)
{
  assert (NULL != data_zone);
  assert (STATE_INITIALIZED == get_device_state (fd));
  bool free_keys = false;

#define KEY_LEN 32
  const unsigned int TEST_KEY_1 = 14;
  const unsigned int TEST_KEY_2 = 15;

  struct octet_buffer test_key_14 = make_buffer (KEY_LEN);
  memset(test_key_14.ptr, 0xAA, KEY_LEN);
  struct octet_buffer test_key_15 = make_buffer (KEY_LEN);
  memset(test_key_15.ptr, 0xBB, KEY_LEN);

  int x = 0;
  /* If there are no keys, which is the case when we are personalizing
     a device from scratch, create some new keys */
  if (NULL == keys)
    {
      keys = make_key_container ();
      for (x=0; x < get_max_keys (); x++)
        {
          if (TEST_KEY_1 == x)
            {
              keys->keys[x] = test_key_14;
            }
          else if (TEST_KEY_2 == x)
            {
              keys->keys[x] = test_key_15;
            }
          else
            {
              keys->keys[x] = get_random (fd, false);
            }
        }

      free_keys = true;
    }

  bool status = true;

  for (x=0; x < get_max_keys () && status; x++)
    {
      const unsigned int WORD_OFFSET = 8;
      unsigned int addr = WORD_OFFSET * x;
      status = write32 (fd, DATA_ZONE, addr, keys->keys[x], NULL);
    }

  if (status)
    {
      data_zone->len = get_max_keys () * keys->keys[0].len;
      data_zone->ptr = malloc_wipe (data_zone->len);

      for (x=0; x < get_max_keys (); x++)
        {
          CTX_LOG(DEBUG, "Writing key %u", x);
          unsigned int offset = x * keys->keys[x].len;
          memcpy(data_zone->ptr + offset, keys->keys[x].ptr, keys->keys[x].len);
        }

      status = record_keys (keys);

    }


  if (free_keys)
    free_key_container (keys);

  return status;

}