block-common.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Common code for block device models
  3. *
  4. * Copyright (C) 2012 Red Hat, Inc.
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or
  7. * later. See the COPYING file in the top-level directory.
  8. */
  9. #include "blockdev.h"
  10. #include "hw/block-common.h"
  11. #include "qemu-error.h"
  12. void blkconf_serial(BlockConf *conf, char **serial)
  13. {
  14. DriveInfo *dinfo;
  15. if (!*serial) {
  16. /* try to fall back to value set with legacy -drive serial=... */
  17. dinfo = drive_get_by_blockdev(conf->bs);
  18. if (dinfo->serial) {
  19. *serial = g_strdup(dinfo->serial);
  20. }
  21. }
  22. }
  23. int blkconf_geometry(BlockConf *conf, int *ptrans,
  24. unsigned cyls_max, unsigned heads_max, unsigned secs_max)
  25. {
  26. DriveInfo *dinfo;
  27. if (!conf->cyls && !conf->heads && !conf->secs) {
  28. /* try to fall back to value set with legacy -drive cyls=... */
  29. dinfo = drive_get_by_blockdev(conf->bs);
  30. conf->cyls = dinfo->cyls;
  31. conf->heads = dinfo->heads;
  32. conf->secs = dinfo->secs;
  33. if (ptrans) {
  34. *ptrans = dinfo->trans;
  35. }
  36. }
  37. if (!conf->cyls && !conf->heads && !conf->secs) {
  38. hd_geometry_guess(conf->bs,
  39. &conf->cyls, &conf->heads, &conf->secs,
  40. ptrans);
  41. } else if (ptrans && *ptrans == BIOS_ATA_TRANSLATION_AUTO) {
  42. *ptrans = hd_bios_chs_auto_trans(conf->cyls, conf->heads, conf->secs);
  43. }
  44. if (conf->cyls || conf->heads || conf->secs) {
  45. if (conf->cyls < 1 || conf->cyls > cyls_max) {
  46. error_report("cyls must be between 1 and %u", cyls_max);
  47. return -1;
  48. }
  49. if (conf->heads < 1 || conf->heads > heads_max) {
  50. error_report("heads must be between 1 and %u", heads_max);
  51. return -1;
  52. }
  53. if (conf->secs < 1 || conf->secs > secs_max) {
  54. error_report("secs must be between 1 and %u", secs_max);
  55. return -1;
  56. }
  57. }
  58. return 0;
  59. }