linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Oliver Neukum <oneukum@suse.com>
To: Ajay Gupta <ajaykuee@gmail.com>, heikki.krogerus@linux.intel.com
Cc: Ajay Gupta <ajayg@nvidia.com>, linux-usb@vger.kernel.org
Subject: [v4,2/2] usb: typec: ucsi: add firmware flashing support
Date: Thu, 07 Feb 2019 12:08:50 +0100	[thread overview]
Message-ID: <1549537730.16571.11.camel@suse.com> (raw)

On Mi, 2019-02-06 at 13:39 -0800, Ajay Gupta wrote:
> From: Ajay Gupta <ajayg@nvidia.com>

Hi,

> 
> CCGx has two copies of the firmware in addition to the bootloader.
> If the device is running FW1, FW2 can be updated with the new version.
> Dual firmware mode allows the CCG device to stay in a PD contract and
> support USB PD and Type-C functionality while a firmware update is in
> progress.
> 
> First we read the currently flashed firmware version of both
> primary and secondary firmware and then compare it with
> version of firmware file to determine if flashing is required.

I am wondering about the security implications of this.
You are persistently changing firmware of a device. It looks
to me like there ought to be a check for capable'(CFAP_SYS_RAWIO)
in the firmware update path.

> Command framework is added to support sending commands to CCGx
> controller. We wait for response after sending the command and then
> read the response from RAB_RESPONSE register.
> 
> Below commands are supported,
>         - ENTER_FLASHING
>         - RESET
>         - PDPORT_ENABLE
>         - JUMP_TO_BOOT
>         - FLASH_ROW_RW
>         - VALIDATE_FW
> 
> Command specific mutex lock is also added to sync between driver
> and user threads.
> 
> PD port number information is added which is required while sending
> PD_PORT_ENABLE command
> 
> Signed-off-by: Ajay Gupta <ajayg@nvidia.com>

[..]

> +struct ccg_cmd {
> +	u16 reg;
> +	u32 data;
> +	int len;
> +	int delay; /* ms delay for cmd timeout  */

unsigned? A negative delay is an interesting concept.

[..]

> +static int ccg_cmd_reset(struct ucsi_ccg *uc, bool extra_delay)
> +{
> +	struct ccg_cmd cmd;
> +	u8 *p;
> +	int ret;
> +
> +	p = (u8 *)&cmd.data;
> +	cmd.reg = CCGX_RAB_RESET_REQ;
> +	p[0] = RESET_SIG;
> +	p[1] = CMD_RESET_DEV;
> +	cmd.len = 2;
> +	cmd.delay = 2000 + (extra_delay ? 3000 : 0);

This is a maximum, right?
If a reset fails, this is very bad. So why not always allow
the maximum timeout?

> +	mutex_lock(&uc->lock);
> +
> +	set_bit(RESET_PENDING, &uc->flags);
> +
> +	ret = ccg_send_command(uc, &cmd);
> +	if (ret != RESET_COMPLETE)
> +		goto err_clear_flag;
> +
> +	ret = 0;
> +
> +err_clear_flag:
> +	clear_bit(RESET_PENDING, &uc->flags);
> +
> +	mutex_unlock(&uc->lock);
> +
> +	return ret;
> +}

[..]

> +static int
> +ccg_cmd_write_flash_row(struct ucsi_ccg *uc, u16 row,
> +			const void *data, u8 fcmd)
> +{
> +	struct i2c_client *client = uc->client;
> +	struct ccg_cmd cmd;
> +	u8 buf[CCG4_ROW_SIZE + 2];
> +	u8 *p;
> +	int ret;
> +
> +	/* Copy the data into the flash read/write memory. */
> +	buf[0] = REG_FLASH_RW_MEM & 0xFF;
> +	buf[1] = REG_FLASH_RW_MEM >> 8;

We have macros for converting endianness. Please use them. That is
a kind of generic issue with this patch.

> +
> +	memcpy(buf + 2, data, CCG4_ROW_SIZE);
> +
> +	mutex_lock(&uc->lock);
> +
> +	ret = i2c_master_send(client, buf, CCG4_ROW_SIZE + 2);
> +	if (ret != CCG4_ROW_SIZE + 2) {
> +		dev_err(uc->dev, "REG_FLASH_RW_MEM write fail %d\n", ret);
> +		return ret < 0 ? ret : -EIO;
> +	}
> +
> +	/* Use the FLASH_ROW_READ_WRITE register to trigger */
> +	/* writing of data to the desired flash row */
> +	p = (u8 *)&cmd.data;
> +	cmd.reg = CCGX_RAB_FLASH_ROW_RW;
> +	p[0] = FLASH_SIG;
> +	p[1] = fcmd;
> +	p[2] = row & 0xFF;
> +	p[3] = row >> 8;

Again. The macros.

> +	cmd.len = 4;
> +	cmd.delay = 50;
> +	if (fcmd == FLASH_FWCT_SIG_WR_CMD)
> +		cmd.delay += 400;
> +	if (row == 510)
> +		cmd.delay += 220;
> +	ret = ccg_send_command(uc, &cmd);
> +
> +	mutex_unlock(&uc->lock);
> +
> +	if (ret != CMD_SUCCESS) {
> +		dev_err(uc->dev, "write flash row failed ret=%d\n", ret);
> +		return ret;
> +	}
> +
> +	return 0;
> +}

             reply	other threads:[~2019-02-07 11:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-07 11:08 Oliver Neukum [this message]
  -- strict thread matches above, loose matches on Subject: below --
2019-02-07 18:57 [v4,2/2] usb: typec: ucsi: add firmware flashing support Ajay Gupta
2019-02-06 21:39 Ajay Gupta

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1549537730.16571.11.camel@suse.com \
    --to=oneukum@suse.com \
    --cc=ajayg@nvidia.com \
    --cc=ajaykuee@gmail.com \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=linux-usb@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).