opal-secvar.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PowerNV code for secure variables
  4. *
  5. * Copyright (C) 2019 IBM Corporation
  6. * Author: Claudio Carvalho
  7. * Nayna Jain
  8. *
  9. * APIs to access secure variables managed by OPAL.
  10. */
  11. #define pr_fmt(fmt) "secvar: "fmt
  12. #include <linux/types.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/of_platform.h>
  15. #include <asm/opal.h>
  16. #include <asm/secvar.h>
  17. #include <asm/secure_boot.h>
  18. static int opal_status_to_err(int rc)
  19. {
  20. int err;
  21. switch (rc) {
  22. case OPAL_SUCCESS:
  23. err = 0;
  24. break;
  25. case OPAL_UNSUPPORTED:
  26. err = -ENXIO;
  27. break;
  28. case OPAL_PARAMETER:
  29. err = -EINVAL;
  30. break;
  31. case OPAL_RESOURCE:
  32. err = -ENOSPC;
  33. break;
  34. case OPAL_HARDWARE:
  35. err = -EIO;
  36. break;
  37. case OPAL_NO_MEM:
  38. err = -ENOMEM;
  39. break;
  40. case OPAL_EMPTY:
  41. err = -ENOENT;
  42. break;
  43. case OPAL_PARTIAL:
  44. err = -EFBIG;
  45. break;
  46. default:
  47. err = -EINVAL;
  48. }
  49. return err;
  50. }
  51. static int opal_get_variable(const char *key, uint64_t ksize,
  52. u8 *data, uint64_t *dsize)
  53. {
  54. int rc;
  55. if (!key || !dsize)
  56. return -EINVAL;
  57. *dsize = cpu_to_be64(*dsize);
  58. rc = opal_secvar_get(key, ksize, data, dsize);
  59. *dsize = be64_to_cpu(*dsize);
  60. return opal_status_to_err(rc);
  61. }
  62. static int opal_get_next_variable(const char *key, uint64_t *keylen,
  63. uint64_t keybufsize)
  64. {
  65. int rc;
  66. if (!key || !keylen)
  67. return -EINVAL;
  68. *keylen = cpu_to_be64(*keylen);
  69. rc = opal_secvar_get_next(key, keylen, keybufsize);
  70. *keylen = be64_to_cpu(*keylen);
  71. return opal_status_to_err(rc);
  72. }
  73. static int opal_set_variable(const char *key, uint64_t ksize, u8 *data,
  74. uint64_t dsize)
  75. {
  76. int rc;
  77. if (!key || !data)
  78. return -EINVAL;
  79. rc = opal_secvar_enqueue_update(key, ksize, data, dsize);
  80. return opal_status_to_err(rc);
  81. }
  82. static const struct secvar_operations opal_secvar_ops = {
  83. .get = opal_get_variable,
  84. .get_next = opal_get_next_variable,
  85. .set = opal_set_variable,
  86. };
  87. static int opal_secvar_probe(struct platform_device *pdev)
  88. {
  89. if (!opal_check_token(OPAL_SECVAR_GET)
  90. || !opal_check_token(OPAL_SECVAR_GET_NEXT)
  91. || !opal_check_token(OPAL_SECVAR_ENQUEUE_UPDATE)) {
  92. pr_err("OPAL doesn't support secure variables\n");
  93. return -ENODEV;
  94. }
  95. set_secvar_ops(&opal_secvar_ops);
  96. return 0;
  97. }
  98. static const struct of_device_id opal_secvar_match[] = {
  99. { .compatible = "ibm,secvar-backend",},
  100. {},
  101. };
  102. static struct platform_driver opal_secvar_driver = {
  103. .driver = {
  104. .name = "secvar",
  105. .of_match_table = opal_secvar_match,
  106. },
  107. };
  108. static int __init opal_secvar_init(void)
  109. {
  110. return platform_driver_probe(&opal_secvar_driver, opal_secvar_probe);
  111. }
  112. device_initcall(opal_secvar_init);