qemu-config-qmp.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #include "qemu/osdep.h"
  3. #include "qapi/error.h"
  4. #include "qapi/qapi-commands-misc.h"
  5. #include "qapi/qmp/qlist.h"
  6. #include "qemu/option.h"
  7. #include "qemu/config-file.h"
  8. #include "hw/boards.h"
  9. static CommandLineParameterInfoList *query_option_descs(const QemuOptDesc *desc)
  10. {
  11. CommandLineParameterInfoList *param_list = NULL;
  12. CommandLineParameterInfo *info;
  13. int i;
  14. for (i = 0; desc[i].name != NULL; i++) {
  15. info = g_malloc0(sizeof(*info));
  16. info->name = g_strdup(desc[i].name);
  17. switch (desc[i].type) {
  18. case QEMU_OPT_STRING:
  19. info->type = COMMAND_LINE_PARAMETER_TYPE_STRING;
  20. break;
  21. case QEMU_OPT_BOOL:
  22. info->type = COMMAND_LINE_PARAMETER_TYPE_BOOLEAN;
  23. break;
  24. case QEMU_OPT_NUMBER:
  25. info->type = COMMAND_LINE_PARAMETER_TYPE_NUMBER;
  26. break;
  27. case QEMU_OPT_SIZE:
  28. info->type = COMMAND_LINE_PARAMETER_TYPE_SIZE;
  29. break;
  30. }
  31. info->help = g_strdup(desc[i].help);
  32. info->q_default = g_strdup(desc[i].def_value_str);
  33. QAPI_LIST_PREPEND(param_list, info);
  34. }
  35. return param_list;
  36. }
  37. /* remove repeated entry from the info list */
  38. static void cleanup_infolist(CommandLineParameterInfoList *head)
  39. {
  40. CommandLineParameterInfoList *pre_entry, *cur, *del_entry;
  41. cur = head;
  42. while (cur->next) {
  43. pre_entry = head;
  44. while (pre_entry != cur->next) {
  45. if (!strcmp(pre_entry->value->name, cur->next->value->name)) {
  46. del_entry = cur->next;
  47. cur->next = cur->next->next;
  48. del_entry->next = NULL;
  49. qapi_free_CommandLineParameterInfoList(del_entry);
  50. break;
  51. }
  52. pre_entry = pre_entry->next;
  53. }
  54. cur = cur->next;
  55. }
  56. }
  57. /* merge the description items of two parameter infolists */
  58. static void connect_infolist(CommandLineParameterInfoList *head,
  59. CommandLineParameterInfoList *new)
  60. {
  61. CommandLineParameterInfoList *cur;
  62. cur = head;
  63. while (cur->next) {
  64. cur = cur->next;
  65. }
  66. cur->next = new;
  67. }
  68. /* access all the local QemuOptsLists for drive option */
  69. static CommandLineParameterInfoList *get_drive_infolist(void)
  70. {
  71. CommandLineParameterInfoList *head = NULL, *cur;
  72. int i;
  73. for (i = 0; drive_config_groups[i] != NULL; i++) {
  74. if (!head) {
  75. head = query_option_descs(drive_config_groups[i]->desc);
  76. } else {
  77. cur = query_option_descs(drive_config_groups[i]->desc);
  78. connect_infolist(head, cur);
  79. }
  80. }
  81. cleanup_infolist(head);
  82. return head;
  83. }
  84. static CommandLineParameterInfo *objprop_to_cmdline_prop(ObjectProperty *prop)
  85. {
  86. CommandLineParameterInfo *info;
  87. info = g_malloc0(sizeof(*info));
  88. info->name = g_strdup(prop->name);
  89. if (g_str_equal(prop->type, "bool") || g_str_equal(prop->type, "OnOffAuto")) {
  90. info->type = COMMAND_LINE_PARAMETER_TYPE_BOOLEAN;
  91. } else if (g_str_equal(prop->type, "int")) {
  92. info->type = COMMAND_LINE_PARAMETER_TYPE_NUMBER;
  93. } else if (g_str_equal(prop->type, "size")) {
  94. info->type = COMMAND_LINE_PARAMETER_TYPE_SIZE;
  95. } else {
  96. info->type = COMMAND_LINE_PARAMETER_TYPE_STRING;
  97. }
  98. if (prop->description) {
  99. info->help = g_strdup(prop->description);
  100. }
  101. return info;
  102. }
  103. static CommandLineParameterInfoList *query_all_machine_properties(void)
  104. {
  105. CommandLineParameterInfoList *params = NULL, *clpiter;
  106. CommandLineParameterInfo *info;
  107. GSList *machines, *curr_mach;
  108. ObjectPropertyIterator op_iter;
  109. ObjectProperty *prop;
  110. bool is_new;
  111. machines = object_class_get_list(TYPE_MACHINE, false);
  112. assert(machines);
  113. /* Loop over all machine classes */
  114. for (curr_mach = machines; curr_mach; curr_mach = curr_mach->next) {
  115. object_class_property_iter_init(&op_iter, curr_mach->data);
  116. /* ... and over the properties of each machine: */
  117. while ((prop = object_property_iter_next(&op_iter))) {
  118. if (!prop->set) {
  119. continue;
  120. }
  121. /*
  122. * Check whether the property has already been put into the list
  123. * (via another machine class)
  124. */
  125. is_new = true;
  126. for (clpiter = params; clpiter != NULL; clpiter = clpiter->next) {
  127. if (g_str_equal(clpiter->value->name, prop->name)) {
  128. is_new = false;
  129. break;
  130. }
  131. }
  132. /* If it hasn't been added before, add it now to the list */
  133. if (is_new) {
  134. info = objprop_to_cmdline_prop(prop);
  135. QAPI_LIST_PREPEND(params, info);
  136. }
  137. }
  138. }
  139. g_slist_free(machines);
  140. /* Add entry for the "type" parameter */
  141. info = g_malloc0(sizeof(*info));
  142. info->name = g_strdup("type");
  143. info->type = COMMAND_LINE_PARAMETER_TYPE_STRING;
  144. info->help = g_strdup("machine type");
  145. QAPI_LIST_PREPEND(params, info);
  146. return params;
  147. }
  148. CommandLineOptionInfoList *qmp_query_command_line_options(const char *option,
  149. Error **errp)
  150. {
  151. CommandLineOptionInfoList *conf_list = NULL;
  152. CommandLineOptionInfo *info;
  153. int i;
  154. for (i = 0; vm_config_groups[i] != NULL; i++) {
  155. if (!option || !strcmp(option, vm_config_groups[i]->name)) {
  156. info = g_malloc0(sizeof(*info));
  157. info->option = g_strdup(vm_config_groups[i]->name);
  158. if (!strcmp("drive", vm_config_groups[i]->name)) {
  159. info->parameters = get_drive_infolist();
  160. } else {
  161. info->parameters =
  162. query_option_descs(vm_config_groups[i]->desc);
  163. }
  164. QAPI_LIST_PREPEND(conf_list, info);
  165. }
  166. }
  167. if (!option || !strcmp(option, "machine")) {
  168. info = g_malloc0(sizeof(*info));
  169. info->option = g_strdup("machine");
  170. info->parameters = query_all_machine_properties();
  171. QAPI_LIST_PREPEND(conf_list, info);
  172. }
  173. if (conf_list == NULL) {
  174. error_setg(errp, "invalid option name: %s", option);
  175. }
  176. return conf_list;
  177. }