block-common.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 "sysemu/blockdev.h"
  10. #include "hw/block-common.h"
  11. #include "qemu/error-report.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. *serial = g_strdup(dinfo->serial);
  19. }
  20. }
  21. int blkconf_geometry(BlockConf *conf, int *ptrans,
  22. unsigned cyls_max, unsigned heads_max, unsigned secs_max)
  23. {
  24. DriveInfo *dinfo;
  25. if (!conf->cyls && !conf->heads && !conf->secs) {
  26. /* try to fall back to value set with legacy -drive cyls=... */
  27. dinfo = drive_get_by_blockdev(conf->bs);
  28. conf->cyls = dinfo->cyls;
  29. conf->heads = dinfo->heads;
  30. conf->secs = dinfo->secs;
  31. if (ptrans) {
  32. *ptrans = dinfo->trans;
  33. }
  34. }
  35. if (!conf->cyls && !conf->heads && !conf->secs) {
  36. hd_geometry_guess(conf->bs,
  37. &conf->cyls, &conf->heads, &conf->secs,
  38. ptrans);
  39. } else if (ptrans && *ptrans == BIOS_ATA_TRANSLATION_AUTO) {
  40. *ptrans = hd_bios_chs_auto_trans(conf->cyls, conf->heads, conf->secs);
  41. }
  42. if (conf->cyls || conf->heads || conf->secs) {
  43. if (conf->cyls < 1 || conf->cyls > cyls_max) {
  44. error_report("cyls must be between 1 and %u", cyls_max);
  45. return -1;
  46. }
  47. if (conf->heads < 1 || conf->heads > heads_max) {
  48. error_report("heads must be between 1 and %u", heads_max);
  49. return -1;
  50. }
  51. if (conf->secs < 1 || conf->secs > secs_max) {
  52. error_report("secs must be between 1 and %u", secs_max);
  53. return -1;
  54. }
  55. }
  56. return 0;
  57. }