OP-TEE / optee_os

Trusted side of the TEE
Other
1.6k stars 1.07k forks source link

Should OP-TEE revision be printed as "string" in linux OP-TEE driver ? #7141

Open sahilnxp opened 5 days ago

sahilnxp commented 5 days ago

Since we have moved to 16bit sha1 len for OP-TEE revision in case of 64 bit architecture.

Should we print the revision as string in place of long int here

Observed one issue on 64 bit architectures, if the commit id is starting with 0 like 04d1c612ec7beaede073b8cad0f33a1f5ab9e2bc

It is printing revision as below removing leading 0. 2024-11-15T02:29:51.469317 [ 2.019585] optee: revision 4.4 (4d1c612ec7beaed)

jforissier commented 5 days ago

Hi @sahilnxp,

Should we print the revision as string in place of long int here

I think the format simply needs adjusting to cater to the size of the unsigned long:

pr_info("revision %lu.%lu (%0*lx)", res.result.major,
    res.result.minor, sizeof(res.result.build_id) * 2,
    res.result.build_id);
sahilnxp commented 5 days ago

Hi @sahilnxp,

Should we print the revision as string in place of long int here

I think the format simply needs adjusting to cater to the size of the unsigned long:

pr_info("revision %lu.%lu (%0*lx)", res.result.major,
  res.result.minor, sizeof(res.result.build_id) * 2,
  res.result.build_id);

Yes, that will work, but we need to pass the size like (16) too in this case like below:

pr_info("revision %lu.%lu (%0*lx)", res.result.major,
    res.result.minor, sizeof(res.result.build_id) * 2,
    16, res.result.build_id);

and how to decide on this size ? Otherwise, it is giving the following warning.

                 from drivers/tee/optee/smc_abi.c:12:
drivers/tee/optee/smc_abi.c: In function ‘optee_msg_get_os_revision’:
./include/linux/kern_levels.h:5:25: warning: field width specifier ‘*’ expects argument of type ‘int’, but argument 4 has type ‘long unsigned int’ [-Wformat=]
    5 | #define KERN_SOH        "\001"          /* ASCII Start Of Header */
      |                         ^~~~~~
jenswi-linaro commented 4 days ago

Casting the size argument to int should work.

sahilnxp commented 4 days ago

Sorry for confusion here.

There is no compilation warning in case of following code:

pr_info("revision %lu.%lu (%0*lx)", res.result.major,
    res.result.minor, sizeof(res.result.build_id) * 2,
    16, res.result.build_id);

But how we can decide if we should give the size as 8 (for 32bit arch) and 16 (for 64 bit arch) ?

jforissier commented 4 days ago

Sorry for confusion here.

There is no compilation warning in case of following code:

pr_info("revision %lu.%lu (%0*lx)", res.result.major,
  res.result.minor, sizeof(res.result.build_id) * 2,
  16, res.result.build_id);

But how we can decide if we should give the size as 8 (for 32bit arch) and 16 (for 64 bit arch) ?

res.result.build_id is unsigned long. 32-bit arch have sizeof(unsigned long) == 4 while 64-bit arch have sizeof(unsigned long) == 8.

sahilnxp commented 4 days ago

So it should be like this, right ?

pr_info("revision %lu.%lu (%0*lx)", res.result.major,
             res.result.minor, (int)sizeof(unsigned long),
             res.result.build_id);
jforissier commented 4 days ago

No, because in the Linux (and OP-TEE) coding style, the argument of sizeof() should be the variable rather than the type. This makes sense to make maintenance easier in case the type is changed for instance.

sahilnxp commented 4 days ago

In that case, it should be like below ?

pr_info("revision %lu.%lu (%0*lx)", res.result.major,
             res.result.minor, (int)sizeof(res.result.build_id),
             res.result.build_id);
jforissier commented 3 days ago

In that case, it should be like below ?

pr_info("revision %lu.%lu (%0*lx)", res.result.major,
             res.result.minor, (int)sizeof(res.result.build_id),
             res.result.build_id);

No. The * 2 is missing. Here is exactly how the code should be:

    if (res.result.build_id)
        pr_info("revision %lu.%lu (%08lx)", res.result.major,
            res.result.minor, (int)sizeof(res.result.build_id) * 2,
            res.result.build_id);
    else
        pr_info("revision %lu.%lu", res.result.major, res.result.minor);